AnalystPath

Employee Manager Hierarchy Depth

SQLHardSenior level~15 min

Problem

**[Classic — recursive CTE]**

The `employees` table has a `manager_id` referencing another employee.
Calculate each employee's **depth** in the org chart (CEO has manager_id=NULL
and depth=1).

Return `id`, `name`, `depth`, ordered by depth then id.

Tables

Example rows — the live problem includes the full dataset.

employees
idnamemanager_id

Expected output

Your answer should return 6 rows with the columns id, name, depth.

Starter code (SQL)

SELECT *
FROM employees;

Solve this SQL question free

Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related SQL questions