How to use self joins
· Category: SQL & Databases
Short answer
A self join joins a table to itself using different aliases, enabling queries about relationships between rows in the same table.
Steps
- Assign aliases:
SELECT e.name, m.name AS manager FROM employees e JOIN employees m ON e.manager_id = m.id; - Use LEFT JOIN to include rows without a match, such as the CEO with no manager.
- Find pairs:
SELECT a.name, b.name FROM employees a JOIN employees b ON a.department = b.department AND a.id != b.id; - Filter by hierarchy depth with recursive CTEs for deep trees.
- Ensure indexes exist on the joined columns.
Tips
- Self joins are useful for organizational hierarchies, graph adjacency lists, and finding duplicates.
- Recursive CTEs are often a better choice for traversing arbitrarily deep hierarchies.
Common issues
- Missing
a.id != b.idin pair queries causes every row to match itself. - NULL foreign keys in hierarchical data break inner self joins unexpectedly.r