Chain of Command

A hard SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
hard
Seniority
L5
Asked in
Visa

Problem

Every employee's manager_id points to their manager's employee_id, except the CEO, whose manager_id is NULL. Return each employee with their depth beneath the CEO and the full chain of names from the CEO down, joined by / like a file path and listed in path order.

The tree runs deep. Trace every branch back to the top.

Tables

employees

Columns: employee_id INTEGER, emp_name TEXT, manager_id INTEGER, department TEXT, salary REAL, hire_date TEXT, user_id INTEGER

employees: 10 of 200 rows
employee_idemp_namedepartmentsalarymanager_idhire_dateuser_id
1Alice ChenEngineering55731NULL2021-02-02100
2Bob MartinezProduct61462NULL2022-03-03197
3Carol WuDesign67193NULL2023-04-04294
4David KimMarketing72924NULL2024-05-05391
5Eve JohnsonSales78655NULL2025-06-06488
6Frank PatelHR84386NULL2026-07-07585
7Grace LeeFinance55117NULL2020-08-08682
8Hank BrownOperations60848NULL2021-09-09779
9Iris DavisData66579NULL2022-10-10876
10Jake WilsonLegal72310NULL2023-11-11973

Expected output

First 5 of 200 rows
employee_idemp_namedepthpath
1Alice Chen0Alice Chen
10000101Alice Chen0Alice Chen
30Dan Baker1Alice Chen/Dan Baker
10000130Dan Baker1Alice Chen/Dan Baker
2Bob Martinez0Bob Martinez

Worked solution and explanation

What this really is

This is a hierarchy walk wearing an org-chart costume. The real skill: a recursive CTE that anchors on the roots (managers with no manager), then joins downward one level at a time, building each person's path as it goes. The trap almost everyone hits is the join direction. Write t.manager_id = e.employee_id and you climb up toward the CEO instead of descending from it, so each person surfaces once at their own level and never carries their subtree's path. Get it backwards on cyclic data and the recursion never terminates.


Break down the requirements

Step 1: Define the anchor (root nodes)

Anchor the recursive CTE on the CEO (manager_id IS NULL), seeding depth=0 and path=emp_name.

Step 2: Define the recursive step

Recursively join employees onto the tree by e.manager_id = t.employee_id, incrementing depth and appending '/' || emp_name to build the file-path-style chain from the CEO down. Note the direction: the NEW row's manager_id matches the ALREADY-visited parent's employee_id.

Step 3: Select the flattened result

Select employee_id, emp_name, depth, and path from the assembled tree, ordered by path so siblings and their subtrees stay contiguous.


The solution

Recursive CTE for hierarchical traversal
WITH RECURSIVE tree AS (
    SELECT employee_id, emp_name, manager_id, 0 AS depth, emp_name AS path
    FROM employees
    WHERE manager_id IS NULL
    UNION ALL
    SELECT e.employee_id, e.emp_name, e.manager_id, t.depth + 1, t.path || '/' || e.emp_name
    FROM employees e
    JOIN tree t ON e.manager_id = t.employee_id
)
SELECT employee_id, emp_name, depth, path
FROM tree
ORDER BY path, employee_id

Common follow-up questions