← Back to SQL, page 6

sql-0106 · Medium mid

Write a query that returns the full chain of managers above a given employee, from the employee up to the top.

Walk the tree upward: anchor on the employee's own row, then repeatedly join to employees on employees.id = previous.manager_id. The recursion stops at the top, where manager_id is NULL and the join finds nothing. Carry a step counter to keep the order.

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.

sql
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:

sql
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;
stepname
0Omar
1Diego
2Asha

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:

sql
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+).

sql
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