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
Planning want the total organisation size under each VP — everybody in their subtree, at any depth, not just the people who report to them directly.
Return one row per VP (the CEO's direct reports) with team_size: how many people sit beneath them, excluding the VP. Order by team_size descending, then by manager.
Result columns · in this order
manager | The VP whose subtree this row summarises. |
team_size | People beneath them at any depth, excluding the VP. |
How to approach it
Anchor on the VPs, set root_id to their own id, and copy root_id unchanged through every recursive step.
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
| manager | team_size |
|---|---|
| Bo Lindqvist | 11 |
| Chen Wei | 5 |
| Dara O'Neill | 2 |
3 rows — all rows shown.
Constraints
team_size excludes the VP themselves, so a VP with three reports and nobody below them scores 3, not 4.team_size descending, then manager ascending.Worked example
The anchor selects the three VPs and sets root_id to each VP's own id. The recursive term then copies r.root_id unchanged while walking down, so every descendant carries the id of the VP whose subtree it belongs to.
Bo's subtree holds twelve rows including Bo herself, so team_size is 11. Chen's holds six, giving 5, and Dara's holds three, giving 2.
The COUNT(*) - 1 is doing that exclusion. Anchoring on manager_id = <vp> instead would avoid the subtraction — but then a VP with no reports at all would vanish from the result rather than reporting zero.
What this tests
Carrying a value unchanged through a recursion so a subtree can be aggregated — the step that turns a tree walk into a report.
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.