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
An access review needs to see the chain of command above every employee — not just their manager, but the whole line up to the CEO, written out so a reviewer can read it.
Return employee_id, full_name, depth and a path like Ada Okafor > Bo Lindqvist > Eve Marsh > Ivy Chen, for the employees at depth 4 or deeper. Order by employee_id.
Result columns · in this order
employee_id | The employee. |
full_name | Their name. |
depth | Levels from the root, where the CEO is 1. |
path | The chain of command from the CEO down to this person, separated by ' > '. |
How to approach it
Anchor on the person whose manager_id IS NULL and build the path string as you walk down.
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 | depth | path |
|---|---|---|---|
| 9 | Ivy Chen | 4 | Ada Okafor > Bo Lindqvist > Eve Marsh > Ivy Chen |
| 10 | Jonas Weber | 4 | Ada Okafor > Bo Lindqvist > Eve Marsh > Jonas Weber |
| 11 | Kira Novak | 4 | Ada Okafor > Bo Lindqvist > Farid Haddad > Kira Novak |
| 12 | Liam Byrne | 4 | Ada Okafor > Chen Wei > Gita Rao > Liam Byrne |
| 13 | Mina Patel | 4 | Ada Okafor > Chen Wei > Gita Rao > Mina Patel |
| 14 | Noor Aziz | 4 | Ada Okafor > Dara O'Neill > Hugo Silva > Noor Aziz |
| 15 | Omar Diaz | 5 | Ada Okafor > Bo Lindqvist > Eve Marsh > Jonas Weber > Omar Diaz |
| 16 | Priya Nair | 5 | Ada Okafor > Bo Lindqvist > Eve Marsh > Jonas Weber > Priya Nair |
| 17 | Quinn Doyle | 5 | Ada Okafor > Bo Lindqvist > Farid Haddad > Kira Novak > Quinn Doyle |
| 18 | Rosa Lima | 5 | Ada Okafor > Chen Wei > Gita Rao > Liam Byrne > Rosa Lima |
| 19 | Sam Okoro | 5 | Ada Okafor > Chen Wei > Gita Rao > Liam Byrne > Sam Okoro |
| 20 | Tara Singh | 6 | Ada Okafor > Bo Lindqvist > Eve Marsh > Jonas Weber > Omar Diaz > Tara Singh |
| 21 | Uzo Eze | 6 | Ada Okafor > Bo Lindqvist > Eve Marsh > Jonas Weber > Omar Diaz > Uzo Eze |
| 22 | Vik Sharma | 7 | Ada Okafor > Bo Lindqvist > Eve Marsh > Jonas Weber > Omar Diaz > Tara Singh > Vik Sharma |
14 rows — scroll inside the table to see them all.
Constraints
manager_id IS NULL. There is exactly one, and = NULL will not find it — that comparison is never true.depth counts levels from the root, so the CEO is 1 and her direct reports are 2. > , with single spaces, and the path includes the employee themselves as the last element.employee_id.Worked example
The anchor is Ada at depth 1 with path = 'Ada Okafor'. The recursive term appends: Bo becomes Ada Okafor > Bo Lindqvist at depth 2, Eve becomes Ada Okafor > Bo Lindqvist > Eve Marsh at depth 3, and Ivy reaches depth 4.
The deepest row is Vik at depth 7, whose path names six managers above him.
Note where the filter goes. WHERE depth >= 4 belongs in the final SELECT, not in the recursive term — putting it inside would stop the walk before it ever reached depth 4, and return nothing at all.
What this tests
Whether you pick the cheaper direction to recurse in, and whether you know that a filter inside the recursive term prunes the walk rather than the output.
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.