Payroll review, step 1: headcount and payroll per department
An SQL exercise from Querydrill. Solve it with a SQL editor in your browser, checked on the rows it returns and again on changed data, and it is checked the moment you submit.
Try it yourself
Finance is preparing next year's budget and asks for the people numbers. Start with the base table: for every department, return the department name, the number of employees and the total salary. A department with no employees must still appear, with 0 and 0.
departments (4 rows)
| id | name |
|---|---|
| 1 | Sales |
| 2 | Engineering |
| 3 | Support |
| 4 | Finance |
employees (11 rows)
| id | name | dept_id | salary | hire_date | manager_id |
|---|---|---|---|---|---|
| 1 | Ava | 2 | 130000 | 2019-03-15 | NULL |
| 2 | Ben | 2 | 105000 | 2020-07-01 | 1 |
| 3 | Cleo | 1 | 82000 | 2021-01-10 | 7 |
| 4 | Dan | 1 | 76000 | 2022-05-23 | 7 |
| 5 | Eve | 3 | 54000 | 2022-09-12 | 11 |
| 6 | Finn | 2 | 98000 | 2021-11-30 | 1 |
| 7 | Gia | 1 | 99000 | 2018-06-04 | NULL |
| 8 | Hugo | 3 | 61000 | 2020-02-18 | NULL |
| 9 | Iris | 4 | 88000 | 2019-10-07 | NULL |
| 10 | Jack | 4 | 72000 | 2023-04-17 | 9 |
| 11 | Kai | NULL | 50000 | 2024-01-08 | 8 |
Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.
What this exercise teaches
COUNT(*) counts rows, and a department without employees still has one row after a LEFT JOIN. COUNT(e.id) counts only the rows where an employee exists, which is why it returns 0 for the empty department.
A hint if you get stuck
Departments first, employees left joined; count a column of the employee, not the row.
Background you need
Common mistakes on this exercise
- COUNT(*) shows 1 for a department with no employees.
- Inner join drops the department with no employees.
Each of these returns a result that looks plausible. The checker catches them because it runs your query on changed data as well.
Keep going on the practice page
Your first 8 exercises on Querydrill are free, with no account. The practice page has the full editor, timed mode and all the exercises in order. A free account shows 3 reference solutions a day; Pro unlocks every exercise, every solution, timed mode and the certificate.
Open this exerciseMore practice on the same topic
More exercises in Case studies
- Revenue report, step 1: revenue per month
- Revenue report, step 2: change on the previous month
- Revenue report, step 3: running total
- Revenue report, step 4: share of the total
- Customer value, step 1: orders and spend per customer
- Customer value, step 2: first order, last order, days active
- Customer value, step 3: segment the customers
- Customer value, step 4: summarize the segments
- Product performance, step 1: units and revenue per product
- Product performance, step 2: revenue per category
- Product performance, step 3: best seller in each category
- Product performance, step 4: products with no paid sales
- Refund review, step 1: refund rate per month
- Refund review, step 2: refund rate per country
- Refund review, step 3: who gets the refunds
- Refund review, step 4: refunded value per category
- Refund review, step 5: the summary line
- Payroll review, step 2: pay against the company average
- Payroll review, step 3: the top earner's share
- Payroll review, step 4: hires per year and running headcount
- Payroll review, step 5: managers and their teams