Full answer
An adjacency list stores each employee's manager in the same table. A self join finds one level; a recursive CTE finds all levels.
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept_id INTEGER REFERENCES departments(id),
manager_id INTEGER REFERENCES employees(id),
salary INTEGER,
hire_date TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
(1,'Asha', 1,NULL,120000,'2019-03-01'),
(2,'Ravi', 1,1, 95000,'2020-07-15'),
(3,'Mei', 1,1, 95000,'2021-01-10'),
(4,'Tom', 2,1, 70000,'2022-05-20'),
(5,'Sara', 2,4, 62000,'2023-02-01'),
(6,'Diego',3,1, 55000,'2021-11-30'),
(7,'Lena', NULL,4, NULL, '2024-06-03'),
(8,'Omar', 3,6, 48000,'2024-09-09');Everyone under Asha (id 1):
WITH RECURSIVE reports(id, name, manager_id, level) AS (
SELECT id, name, manager_id, 1 FROM employees WHERE manager_id = 1
UNION ALL
SELECT e.id, e.name, e.manager_id, r.level + 1
FROM employees e
JOIN reports r ON e.manager_id = r.id
)
SELECT name, level FROM reports ORDER BY level, name;| name | level |
|---|---|
| Diego | 1 |
| Mei | 1 |
| Ravi | 1 |
| Tom | 1 |
| Lena | 2 |
| Omar | 2 |
| Sara | 2 |
Asha's direct reports are level 1. Sara and Lena report to Tom, and Omar reports to Diego, so those three are level 2. The query would keep going for deeper trees with no changes.
Carrying a path string shows the chain and is handy for sorting the tree into display order:
WITH RECURSIVE tree(id, name, path) AS (
SELECT id, name, name FROM employees WHERE manager_id IS NULL
UNION ALL
SELECT e.id, e.name, t.path || ' > ' || e.name
FROM employees e JOIN tree t ON e.manager_id = t.id
)
SELECT path FROM tree ORDER BY path;| path |
|---|
| Asha |
| Asha > Diego |
| Asha > Diego > Omar |
| Asha > Mei |
| Asha > Ravi |
| Asha > Tom |
| Asha > Tom > Lena |
| Asha > Tom > Sara |
Two cautions. If the data accidentally contains a cycle (A manages B, B manages A), the recursion never ends; guard with a depth limit such as WHERE level < 20, or with a path check that stops when an id is already in the path. And on large hierarchies, an index on manager_id keeps each step a quick lookup.
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept_id INTEGER REFERENCES departments(id),
manager_id INTEGER REFERENCES employees(id),
salary INTEGER,
hire_date TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
(1,'Asha', 1,NULL,120000,'2019-03-01'),
(2,'Ravi', 1,1, 95000,'2020-07-15'),
(3,'Mei', 1,1, 95000,'2021-01-10'),
(4,'Tom', 2,1, 70000,'2022-05-20'),
(5,'Sara', 2,4, 62000,'2023-02-01'),
(6,'Diego',3,1, 55000,'2021-11-30'),
(7,'Lena', NULL,4, NULL, '2024-06-03'),
(8,'Omar', 3,6, 48000,'2024-09-09');
WITH RECURSIVE reports(id, name, manager_id, level) AS (
SELECT id, name, manager_id, 1 FROM employees WHERE manager_id = 1
UNION ALL
SELECT e.id, e.name, e.manager_id, r.level + 1
FROM employees e
JOIN reports r ON e.manager_id = r.id
)
SELECT name, level FROM reports ORDER BY level, name;
WITH RECURSIVE tree(id, name, path) AS (
SELECT id, name, name FROM employees WHERE manager_id IS NULL
UNION ALL
SELECT e.id, e.name, t.path || ' > ' || e.name
FROM employees e JOIN tree t ON e.manager_id = t.id
)
SELECT path FROM tree ORDER BY path;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Anchor: direct reports of the starting manager
- Recursive step: join employees to the previous level on manager_id
- Carry level and path columns for depth and display order
- Guard against cycles with a depth limit or path check
Common mistakes
- Writing one self join per level and missing deeper levels
- Recursing without a cycle guard on data that is not guaranteed to be a tree
Follow-ups
- How would you find the chain of managers above Omar?
- How would you count everyone under each manager?
Tips
- Mention cycles; it shows you have dealt with dirty hierarchy data