Write a query that returns every employee alongside their depth level
in the org chart: the CEO (no manager) is level 0, their direct reports
are level 1, and so on.
Table: `employees`
| id | name | manager_id |
|---|---|---|
| 1 | CEO | NULL |
| 2 | VP Eng | 1 |
| 3 | VP Sales | 1 |
| 4 | Eng Manager | 2 |
| 5 | Engineer A | 4 |
| 6 | Engineer B | 4 |
| 7 | Sales Rep | 3 |
Expected output (columns: id, name, level), ordered by id:
1|CEO|0
2|VP Eng|1
3|VP Sales|1
4|Eng Manager|2
5|Engineer A|3
6|Engineer B|3
7|Sales Rep|2Hint — use
WITH RECURSIVE: the base case is the row(s) with
manager_id IS NULLat level 0, the recursive case joinsemployeesback onto the CTE through
manager_idand adds 1 to the level.
Sample tests