Full answer
Going up a tree is the mirror of going down. Instead of finding rows whose manager is the current row, find the row that is the current row's manager.
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');The chain above Omar:
WITH RECURSIVE chain(id, name, manager_id, step) AS (
SELECT id, name, manager_id, 0 FROM employees WHERE name = 'Omar'
UNION ALL
SELECT m.id, m.name, m.manager_id, c.step + 1
FROM employees m
JOIN chain c ON m.id = c.manager_id
)
SELECT step, name FROM chain ORDER BY step;| step | name |
|---|---|
| 0 | Omar |
| 1 | Diego |
| 2 | Asha |
Omar reports to Diego, who reports to Asha. At Asha, manager_id is NULL, the join returns no row, and the recursion ends by itself, so this direction needs no explicit stop condition as long as the data has no cycles.
A compact one-line answer for display:
WITH RECURSIVE chain(id, name, manager_id, step) AS (
SELECT id, name, manager_id, 0 FROM employees WHERE name = 'Omar'
UNION ALL
SELECT m.id, m.name, m.manager_id, c.step + 1
FROM employees m JOIN chain c ON m.id = c.manager_id
)
SELECT GROUP_CONCAT(name, ' > ' ORDER BY step) AS chain FROM chain;| chain |
|---|
| Omar > Diego > Asha |
The ORDER BY inside the derived table controls the order of names in the chain. SQLite documents that group_concat follows the order of its input rows in this simple form, but the portable way is the aggregate ORDER BY clause (string_agg(name, ' > ' ORDER BY step) in PostgreSQL, also accepted by SQLite 3.44+).
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 chain(id, name, manager_id, step) AS (
SELECT id, name, manager_id, 0 FROM employees WHERE name = 'Omar'
UNION ALL
SELECT m.id, m.name, m.manager_id, c.step + 1
FROM employees m
JOIN chain c ON m.id = c.manager_id
)
SELECT step, name FROM chain ORDER BY step;
WITH RECURSIVE chain(id, name, manager_id, step) AS (
SELECT id, name, manager_id, 0 FROM employees WHERE name = 'Omar'
UNION ALL
SELECT m.id, m.name, m.manager_id, c.step + 1
FROM employees m JOIN chain c ON m.id = c.manager_id
)
SELECT GROUP_CONCAT(name, ' > ' ORDER BY step) AS chain FROM chain;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Walking up: join on employees.id = previous.manager_id
- Stops naturally at the root where manager_id is NULL
- Carry a step number to preserve order
- Use an ordered aggregate to build a readable chain
Common mistakes
- Reversing the join condition and walking down instead of up
- Relying on unordered string aggregation for the chain
Follow-ups
- How would you find the lowest common manager of two employees?
- How would you store a hierarchy so ancestor queries need no recursion?
Tips
- Contrast walking up with walking down in one sentence