Headcount by department, empty and unassigned included
The task
The people team needs a headcount table that accounts for every department and every person.
Count current employees only: those whose termination_date is NULL. Group them by department:
- an employee whose
department_idis NULL is reported under the label'No department' - a department with no current employees must still appear, with a headcount of 0
Return department_name (the department name, or 'No department') and headcount. Sort by department_name.
Expected output
department_nameheadcount- Sorted by department_name 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
- April spend and cost per click for search and socialeasy
- Email-referred customers and their completed orderseasy
- Invoiced amount by the plan active on the issue datemedium
- Salary in effect at review time, by ratinghard
- Spending by merchant partner group, NULL merchants includedmedium
- Urgent email and phone tickets by team, with unassignedeasy
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL