Lesson 24/100

Tutorials MySQL Tutorial

SELF JOIN — Complete Guide

SELF JOIN — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of MySQL Tutorial on Toolliyo Academy.

On this page

MySQL Tutorial · Lesson 24 of 100

SELF JOIN

BasicsAdvanced

Basics · 1 — SQL · ~6 min · MySQL — Joins & Relationships

What is this?

SELF JOIN joins a table to itself with aliases. Common for hierarchies: employee → manager, category → parent category.

Why should you care?

Org charts and referral trees in fintech apps store parent_id in the same employees or users table.

See it live — copy this example

Run in MySQL Workbench or the mysql CLI.

SELECT e.full_name AS employee,
       m.full_name AS manager
FROM employees e
LEFT JOIN employees m ON m.employee_id = e.manager_id;

What happened?

  • e is the worker row; m is another row in employees linked by manager_id.
  • NULL manager means top-level role.

Practice next

  1. CREATE employees with manager_id self-FK.
  2. Insert CEO (NULL manager) and two reports.
  3. Run SELF JOIN; verify hierarchy.
  4. Add WHERE m.full_name IS NULL for top bosses only.
  5. COUNT reports: GROUP BY m.employee_id.

Remember

Same table, two aliases. Models trees with parent_id. LEFT JOIN for optional parent.

DataFlow sales hierarchy

Commission rollup uses SELF JOIN to attach each rep to regional manager.

Outcome: Payroll CSV matches org structure.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Junior Detailed
Explain SQL queries in the context of MySQL.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define SQL queri…
Mid Detailed
What are common mistakes teams make with Schema design when using MySQL?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Schema de…
Senior Detailed
How would you debug a production issue related to Transactions in a MySQL application?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Transacti…
Junior Detailed
Describe a real-world scenario where Normalization mattered in a MySQL project.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Normaliza…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

MySQL Tutorial
Course syllabus

MySQL Tutorial

MySQL — Foundations
MySQL — Queries & Clauses
MySQL — Joins & Relationships
MySQL — Functions & Window Functions
MySQL — Transactions & Concurrency
MySQL — Stored Procedures & Triggers
MySQL — Indexing & Performance
MySQL — Advanced MySQL
MySQL — Security & Cloud MySQL
MySQL — Real-World Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details