Advanced · Lesson 7 of 18

Self Joins

A self join joins a table to itself. You use aliases to treat the same table as two separate tables.

This is useful for hierarchical data like employee/manager relationships:

SELECT e.first_name AS employee,
       m.first_name AS manager
FROM employees e
LEFT JOIN employees m
ON e.manager_id = m.id;

Key points:

  • You MUST use aliases to distinguish the two copies
  • LEFT JOIN ensures employees without a match still appear
  • Common for org charts, referral chains, and category trees

Self joins are a powerful pattern once you understand them.

Check your understanding

What is a self join?

Examples run on the employees sample database. Open any of them in the playground to experiment.