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
A leadership summary wants the top of the Engineering org only: the VP's reports and their reports, and nothing deeper. The full subtree is eleven people across five levels, which is more than the slide can hold.
Return the people within two levels below employee_id = 2, with how far down they sit. Exclude the VP. Order by levels_down, then employee_id.
Result columns · in this order
employee_id | An employee within two levels of the VP. |
full_name | Their name. |
levels_down | 1 for a direct report, 2 for their reports. |
How to approach it
Put the bound in the recursive term's WHERE clause so the walk stops, and exclude the anchor row at the end.
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 | levels_down |
|---|---|---|
| 5 | Eve Marsh | 1 |
| 6 | Farid Haddad | 1 |
| 9 | Ivy Chen | 2 |
| 10 | Jonas Weber | 2 |
| 11 | Kira Novak | 2 |
5 rows — all rows shown.
Constraints
levels_down = 0 and must not appear in the output — exclude her in the final SELECT, not in the anchor, because the walk starts from her.levels_down, then employee_id.Worked example
The anchor is the VP at levels_down = 0. The recursive term carries WHERE s.levels_down < 2, so it expands rows at level 0 and level 1 and refuses to expand rows already at level 2 — which is what makes it stop.
The result is Eve Marsh and Farid Haddad at level 1, then Ivy Chen, Jonas Weber and Kira Novak at level 2. Omar Diaz, Priya Nair and Quinn Doyle at level 3 are never produced at all.
Compare the two placements: WHERE levels_down <= 2 in the final SELECT returns exactly the same five rows, having first walked to level 5 and thrown six rows away. On a deep hierarchy that difference is the whole query cost.
What this tests
Where a bound belongs in a recursive query, and whether you know that a depth cap is also the guard against a cycle that never terminates.
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.