Everyone who reports to a manager, at any depth
The task
Wen Becker (employee_id 2) is leaving on sabbatical and the people team needs everyone in their organisation to hear about the cover plan. That means direct reports, the people who report to them, and so on down the line.
List every current employee (no termination_date) who sits below employee 2 at any depth. Employee 2 is not in the list. A person who has left is not listed, but the people who report to them are still in employee 2's organisation and are listed.
Return employee_id, full_name and levels_below (1 for a direct report, 2 for a report of a direct report, and so on), sorted by levels_below and then employee_id.
Expected output
employee_idfull_namelevels_below- Sorted by levels_below ascending, then employee_id ascending.
- Checked on the sample and on 2 hidden datasets. How grading works
Tables People team · click a name to insert it
departments7 rows in the sample
One row per department. Key: department_id.
| int | Unique id. | |
| text | Name. | |
| text | Main office city. |
employees120 rows in the sample
Everyone who has worked here, including people who left. Key: employee_id.
| int | Unique id. | |
| text | Name. | |
| int | Department. NULL for a few people between teams. | |
| int | Their manager (an employee_id). NULL for the CEO. | |
| text | Title. | |
| date | First day. | |
| date | Last day. NULL while employed. | |
| numeric(10,2) | Current (or final) yearly salary, USD. |
salary_changes367 rows in the sample
Every salary someone has had, from the day it applied. Key: employee_id, effective_on.
| int | The employee. | |
| date | First day of this salary. | |
| numeric(10,2) | Yearly salary, USD. |
reviews301 rows in the sample
Year-end review ratings. Key: employee_id, review_year.
| int | The employee. | |
| int | Year reviewed. | |
| int | 1 (low) to 5 (high). |
Related questions
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL