A self-join means joining a table to itself.
Example: employees and their managers are stored in the same table.
employees --------- id name manager_id
Query:
SELECT e.name AS employee,
m.name AS manager
FROM employees e
LEFT JOIN employees m
ON e.manager_id = m.id;We use aliases because the same table appears twice.
Common use cases:
- Employee → manager relationships
- Hierarchies
- Finding related records
- Comparing rows within the same table