Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
HR need the full org under the VP of Engineering — not just her direct reports, but everybody beneath her at any depth, for a headcount review.
Return every employee under employee_id = 2, with how many levels below her they sit. Direct reports are depth 1. Order by depth, then employee_id.
Result columns · in this order
employee_id | The employee beneath the VP. |
full_name | Their name. |
title | Their job title. |
depth | Levels below the VP: 1 for a direct report, 2 for their reports, and so on. |
How to approach it
Anchor on the direct reports, then join employees back to the CTE on manager_id = the CTE's employee_id.
Sample input
| employee_id | full_name | manager_id | title | department |
|---|---|---|---|---|
| 1 | Ada Okafor | null | CEO | Executive |
| 2 | Bo Lindqvist | 1 | VP Engineering | Engineering |
| 3 | Chen Wei | 1 | VP Data | Data |
| 4 | Dara O'Neill | 1 | VP Sales | Sales |
| 5 | Eve Marsh | 2 | Director, Platform | Engineering |
| 6 | Farid Haddad | 2 | Director, Product Eng | Engineering |
| 7 | Gita Rao | 3 | Director, Analytics | Data |
| 8 | Hugo Silva | 4 | Director, EMEA | Sales |
| 9 | Ivy Chen | 5 | Staff Engineer | Engineering |
| 10 | Jonas Weber | 5 | Engineering Manager | Engineering |
| 11 | Kira Novak | 6 | Engineering Manager | Engineering |
| 12 | Liam Byrne | 7 | Analytics Manager | Data |
| 13 | Mina Patel | 7 | Data Engineer | Data |
| 14 | Noor Aziz | 8 | Account Executive | Sales |
| 15 | Omar Diaz | 10 | Senior Engineer | Engineering |
| 16 | Priya Nair | 10 | Engineer | Engineering |
| 17 | Quinn Doyle | 11 | Engineer | Engineering |
| 18 | Rosa Lima | 12 | Analyst | Data |
| 19 | Sam Okoro | 12 | Analyst | Data |
| 20 | Tara Singh | 15 | Engineer | Engineering |
| 21 | Uzo Eze | 15 | Engineer | Engineering |
| 22 | Vik Sharma | 20 | Junior Engineer | Engineering |
22 rows — scroll inside the table to see them all.
Expected output
| employee_id | full_name | title | depth |
|---|---|---|---|
| 5 | Eve Marsh | Director, Platform | 1 |
| 6 | Farid Haddad | Director, Product Eng | 1 |
| 9 | Ivy Chen | Staff Engineer | 2 |
| 10 | Jonas Weber | Engineering Manager | 2 |
| 11 | Kira Novak | Engineering Manager | 2 |
| 15 | Omar Diaz | Senior Engineer | 3 |
| 16 | Priya Nair | Engineer | 3 |
| 17 | Quinn Doyle | Engineer | 3 |
| 20 | Tara Singh | Engineer | 4 |
| 21 | Uzo Eze | Engineer | 4 |
| 22 | Vik Sharma | Junior Engineer | 5 |
11 rows — scroll inside the table to see them all.
Constraints
WITH RECURSIVE. The anchor selects the direct reports; the recursive term joins the table back to the rows the CTE has already produced.depth must be computed as you walk, not hard-coded from the titles: the tree does not line up with seniority, and one staff engineer sits at the same level as two managers.depth, then employee_id.Worked example
The anchor returns Eve Marsh (5) and Farid Haddad (6) at depth 1. The recursive term then finds everyone whose manager_id is 5 or 6 — Ivy Chen, Jonas Weber and Kira Novak at depth 2 — and repeats with those, reaching Omar Diaz, Priya Nair and Quinn Doyle at depth 3.
It keeps going: Tara Singh and Uzo Eze at depth 4, and Vik Sharma at depth 5. Eleven people in total.
Three self joins would have stopped at depth 3 and returned eight rows — a plausible-looking answer that silently omits the three deepest people. That is the failure this question exists to make visible.
What this tests
Whether you can write a recursive CTE at all, and whether you noticed that the depth of the tree is a property of the data rather than of the query.
Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.