Loading the editor…
Count current employees in each department.
Finance is tying the headcount workbook to the department list. Every department with people should appear once.
Return department and headcount. Count employees per department. Sort by headcount descending, then department name.
| id | name | location |
|---|---|---|
| 1 | Engineering | Berlin |
| 2 | Sales | London |
| 3 | People | New York |
| id | name | department_id | manager_id | hire_date | title | |
|---|---|---|---|---|---|---|
| 1 | Amira Hassan | amira@querylab.example | 1 | NULL | 2021-03-01 | Staff Engineer |
| 2 | Leo Martins | leo@querylab.example | 1 | 1 | 2022-06-15 | Software Engineer |
| 3 | Priya Nair | priya@querylab.example | 1 | 1 | 2023-01-09 | Software Engineer |
| id | employee_id | amount | effective_date |
|---|---|---|---|
| 1 | 1 | 145000 | 2024-01-01 |
| 2 | 1 | 132000 | 2023-01-01 |
| 3 | 2 | 98000 | 2024-01-01 |
| id | employee_id | title | department_id | start_date | end_date |
|---|---|---|---|---|---|
| 1 | 2 | Junior Engineer | 1 | 2022-06-15 | 2023-12-31 |
| 2 | 3 | Junior Engineer | 1 | 2023-01-09 | 2024-03-31 |
| 3 | 5 | SDR | 2 | 2023-04-17 | 2024-06-30 |
Ctrl or Cmd + Enter runs the query. The database resets on every run.
Run the query to see rows from the sample database.
Next exercise: Active MRR by plan
Related: Employees and departments, Departments with more than two people