Classifying Departments in an Org Chart
Problem
A company stores its department hierarchy in `OrgUnits`. Each row has a department id `unit_id` and the id of the department it reports to, `reports_to`. The single top department reports to nobody, so its `reports_to` is NULL. Classify every department into one column `position`:
- `Top` if the department reports to nobody.
- `Middle` if at least one other department reports to it (and it itself reports to someone).
- `Frontline` if no department reports to it.
Return `unit_id` and `position`, ordered by `unit_id` ascending.
Tables
Example rows — the live problem includes the full dataset.
| unit_id | reports_to |
|---|---|
| 11 | 22 |
| 33 | 22 |
| 66 | 88 |
Expected output
Your answer should return 7 rows with the columns unit_id, position.
Starter code (SQL)
SELECT *
FROM OrgUnits;Solve this SQL question free
Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock