SQL Self Join Explained (Employee → Manager Example)

A self join queries the same table twice with aliases — classic employee/manager hierarchy and comparing rows to each other.

orders order_id customer_id ← status JOIN customers → customer_id first_name city row
A JOIN stitches tables together using a shared key (here: customer_id).

Same table, two roles

A self join is still a JOIN — you just alias the table twice. A common example: list each employee with their manager’s name.

-- Conceptual HR table
SELECT e.name AS employee,
       m.name AS manager
FROM employees e
LEFT JOIN employees m
  ON e.manager_id = m.employee_id;

Use LEFT JOIN so the top boss (no manager) still appears.

On the shop dataset

Self joins also compare rows: orders paired with other orders for the same customer, products compared within a category, etc. Always give clear aliases (e/m, a/b) so columns don’t collide.

A simple habit

When you need “same table” + “related rows,” sketch two boxes with different aliases (e / m) before writing the ON clause. It keeps the relationship clear.

FAQ

What is a self join in SQL?

Joining a table to itself using two aliases so you can relate rows inside the same table — like employees to their managers.

Try it yourself

The fastest way to lock this in is a short drill on the same Northstar Market tables.

Practice JOINs

Related guides