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 reorg conversation needs the shape of the org: how many people sit at each level, from the CEO down. Nobody has recorded a level anywhere — the only structure in the table is manager_id.
Return one row per level with the number of people at it. The CEO is level 1. Order by depth.
Result columns · in this order
depth | Level in the org, where the CEO is 1. |
people | How many employees sit at that level. |
How to approach it
Produce (employee_id, depth) for everyone with a recursive walk, then GROUP BY depth outside it.
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
| depth | people |
|---|---|
| 1 | 1 |
| 2 | 3 |
| 3 | 4 |
| 4 | 6 |
| 5 | 5 |
| 6 | 2 |
| 7 | 1 |
7 rows — all rows shown.
Constraints
title or department — one staff engineer sits at the same level as two managers, and Sales is three levels shallower than Engineering.depth.Worked example
Level 1 is the CEO alone. Level 2 is the three VPs. Level 3 is the four directors, and level 4 has six people — the widest layer in the company. Then it narrows: five at level 5, two at level 6, and one at level 7. That single row at the bottom is Vik, seven levels from the CEO in a company of twenty-two people. Seven rows out of twenty-two employees, and the counts must sum back to 22. If they sum to 21, the anchor forgot the CEO; if a level is missing entirely, the walk stopped early.
What this tests
Deriving a value that exists nowhere in the table, and knowing that the aggregation belongs outside the recursive CTE rather than inside it.
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.