Skip to main content
Albero Academy

Self Joins

Join a table to itself to model hierarchies and pairs.

Joins & Combining7 min

A self join joins a table to itself using two aliases. It's how you relate rows within one table — an employee to their manager, a product to a related product.

Employee → manager

two aliases of one table
SELECT e.name AS employee, m.name AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.id;
-- LEFT JOIN so the CEO (no manager) still appears

Key takeaways

  1. 01A self join joins a table to itself using two different aliases.
  2. 02It models within-table relationships: employee/manager, parent/child.
  3. 03Use LEFT JOIN for hierarchies so the top (parent-less) row stays.
  4. 04Self joins can also find pairs of rows sharing an attribute.

Finished this chapter?

Mark it complete to track your progress through SQL.