Sub topic 3
Self Joins: Treating One Table as Two
Prerequisites and the working idea
This chapter assumes you can write basic SELECT statements, use INNER JOIN and LEFT JOIN, and understand primary keys, foreign keys, and NULL.
A self join is a join in which a table is joined to itself. The table appears twice in the query, but each appearance has a different role. For example, one row in an employees table can represent an employee, while another row in the same table represents that employee's manager.
A table alias is a temporary name for a table in one query. You need aliases because SQL must distinguish the two appearances of the same table.
Why aliases matter
Consider this table:
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
manager_id INT REFERENCES employees(id)
);
INSERT INTO employees (id, name, manager_id) VALUES
(1, 'Ava', NULL),
(2, 'Ben', 1),
(3, 'Chen', 1),
(4, 'Dana', 2);The manager_id column stores the id of another employee. This is a foreign key that points back to the same table. Ava has no manager, Ben and Chen report to Ava, and Dana reports to Ben.
In a query, give each table occurrence an alias:
SELECT e.name AS employee,
m.name AS manager
FROM employees AS e
INNER JOIN employees AS m
ON e.manager_id = m.id;Here, e means “the employee occurrence” and m means “the manager occurrence.” The qualified column e.name comes from the first occurrence. The qualified column m.name comes from the second.
The ON condition matches the employee's stored manager ID with the manager row's primary key:
e.manager_idis the employee's reference.m.idis the referenced manager ID.
Because this is an INNER JOIN, employees without a matching manager are excluded. The result contains Ben, Chen, and Dana, but not Ava.
Reading a self-join result
Use column aliases to make the two roles clear:
SELECT e.id AS employee_id,
e.name AS employee,
m.id AS manager_id,
m.name AS manager
FROM employees AS e
INNER JOIN employees AS m
ON e.manager_id = m.id
ORDER BY e.id;The result is conceptually:
| employee_id | employee | manager_id | manager |
|---|---|---|---|
| 2 | Ben | 1 | Ava |
| 3 | Chen | 1 | Ava |
| 4 | Dana | 2 | Ben |
A self join does not copy the table. It creates a result by matching rows from two logical roles. Each role can have its own filters and selected columns.
Common errors
A frequent mistake is joining the wrong columns:
ON e.manager_id = m.manager_idThis compares two employees' manager references. It does not find the manager row. The correct relationship is:
ON e.manager_id = m.idAnother mistake is reversing the roles. If you select m.name as the employee and e.name as the manager, the query may still run, but the labels describe the wrong rows.
Self joins usually show one relationship level. The employee-manager query finds each employee's direct manager. It does not automatically find a manager's manager. Queries that must walk through an unknown number of levels are usually better candidates for a recursive common table expression.
Exercise
Write a query that returns each employee's name and direct manager's name, including only employees who have a manager.
Show answer
SELECT e.name AS employee, m.name AS manager FROM employees AS e INNER JOIN employees AS m ON e.manager_id = m.id;