Stop 06 · Joining TablesTopics · 5/10

SELF JOIN

Part of the SQL Roadmap

In short

An employees table where each row has a manager_id that points to another employee in the same table. To show 'who reports to whom', the table has to join to itself.

A self join joins a table to itself using two aliases, which is useful for hierarchies like employees and their managers, or comparing rows in the same table.

The jaadu sits at an office desk with two identical thick registers open side by side, running one finger down each and connecting them with a long red thread.

Think of it like this

Two photocopies of the staff register

An office in Hyderabad keeps one staff register, and each line says who that person reports to, by staff number. To write the manager's name next to each person, you photocopy the register, lay the copy beside the original, and look up each manager number in the copy. A self join is that photocopy trick, using two aliases for one table.

Watch it happen

Each employee looks up their manager in the same table

1 Priya (mgr none)
0
2 Arjun (mgr 1)
1
3 Neha (mgr 1)
2
4 Vikram (mgr 2)
3

1/5One employees table. manager_id points at another row's emp_id.

The big ideas

What you need to know

One table, two aliases

employees AS e and employees AS m are the same table playing two roles.

Perfect for hierarchies

Manager and employee, parent and child category, referrer and referred customer.

LEFT keeps the top

The CEO has no manager. An INNER self join drops her. LEFT JOIN keeps her with NULL.

Comparing rows

Self joins also find pairs, like two customers in the same city. Use a.id < b.id to avoid duplicates.

See it in code

Walk through the code

sql
1SELECT2  e.emp_id,3  e.name         AS employee,4  e.designation,5  COALESCE(m.name, '(top of org)') AS manager6FROM employees AS e7LEFT JOIN employees AS m8  ON m.emp_id = e.manager_id9ORDER BY manager, employee;

1/3The same table twice: e for the employee, m for the manager.

Every employee next to their manager's name. Works the same in PostgreSQL, MySQL and SQL Server.

Save yourself the pain

Common mistakes

Oops

ON e.emp_id = m.manager_id

Do this instead

That finds each person's reports, not their manager. Read it out loud: the manager's ID equals my manager_id.

Oops

Finding customer pairs in the same city with a.customer_id <> b.customer_id.

Do this instead

You get every pair twice (A-B and B-A). Use a.customer_id < b.customer_id.

Oops

Using an INNER self join for an org chart.

Do this instead

The top person disappears. Use LEFT JOIN so every employee is listed.

Your mission

Org chart in a table

~20 min

Quick check

Why does a self join need two different aliases?

Go deeper

  1. 1Show each employee next to their manager's name.
  2. 2Find pairs of customers who live in the same city.
  3. 3Use clear aliases like e and m so the query stays readable.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge