Problem · Hard · 30 min
Who works below the head
- Hard
- Recursive queries
A company keeps its people in the table employees.
| Column | Meaning |
|---|---|
id |
The number of the person. It is unique. |
name |
The name of the person. No two people have the same name. |
manager_id |
The id of the manager of this person, or NULL if the person has no manager. |
The person with id 1 is the head of the company. Write one query that returns everyone who works below the head: people who report to the head, people who report to those people, and so on. Do not return the head. The columns are:
name: the name of the person.level: 1 for a person who reports directly to the head, 2 for a person who reports to a person of level 1, and so on.
Sort the rows by level, then by name.
Some people in the table can have no manager although they are not the head. They do not work below the head, so they and their teams are not in the result. The table has no cycles.
Tasks
2 to do
Task 1, not checked yet. Return the right rows for the example data.
Task 2, not checked yet. Return the right rows for other data that you cannot see.
Task 1 of 2
Return the right rows for the example data.