← SQL Deep Dive
15 / 25
Lesson 15 — Self join
Self join: a table joined to itself
A self join joins a table to itself using two different aliases. Classic case: employees have a manager_id that points to another row of the same table. Alias e is "the employee", alias m is "their manager". A LEFT JOIN keeps the boss, who has no manager.
SELECT p.name, par.name
FROM people p LEFT JOIN people par ON p.parent_id = par.id
Why it matters: org charts, category trees and "replies to comments" are all stored as rows pointing to rows in the same table.
Want it another way?
Task
Fill in the ON condition linking each employee to their manager. Expected: Ann has no manager (None).
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
Test your knowledge
A quick 3-question quiz on this lesson — instant feedback.