SELF JOIN
6. SELF JOIN
Section titled “6. SELF JOIN”What it does
Section titled “What it does”Joins a table with itself. You use table aliases to treat the same table as if it were two separate tables. This is essential for hierarchical or recursive data like org charts, category trees, or social networks.
Visual Diagram
Section titled “Visual Diagram”-- Find each employee and their manager's nameSELECT e.name AS employee, m.name AS managerFROM employees eLEFT JOIN employees m ON e.manager_id = m.id;Result
Section titled “Result”| employee | manager |
|---|---|
| Alice | NULL |
| Bob | Alice |
| Carol | Alice |
| David | Bob |
How it works
Section titled “How it works”The key is aliasing the same table twice — e for “the employee row” and m for “the manager row.” The join condition e.manager_id = m.id follows the reference from the employee’s manager_id back to another row in the same table.
We use a LEFT JOIN (not INNER JOIN) so that Alice (who has no manager) still appears in the result with NULL.
Other self-join use cases
Section titled “Other self-join use cases”- Category trees:
parent_idreferencesidin the same categories table - Friend networks:
user_idandfriend_idboth reference theuserstable - Version chains: each record has a
previous_version_idpointing to an earlier row