SQL · Pattern 6 — Joins
SELF JOIN
A SELF JOIN joins a table to itself using different aliases.
Source data — Pattern 6: Joins
All queries run against the shared employees and departments tables (see schema.sql). This pattern introduces the departments table. Topic 38 also needs a projects table, while topic 39 introduces a salary_grades table for non-equi joins.




What it does
A SELF JOIN joins a table to itself using different aliases. It is commonly used to relate employees to their managers.
Problem
Find employees and their managers.
SELECT e.first_name AS employee,
m.first_name AS manager
FROM employees e
LEFT JOIN employees m
ON e.manager_id = m.employee_id;
Result
