Payroll review, step 4: hires per year and running headcount
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
How did the team grow? For every year in which someone was hired, return the year as a number, the number of hires in that year and the running total of hires up to and including that year.
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
A window function can run over grouped rows: COUNT(*) is the hires of the year, and SUM(COUNT(*)) OVER (ORDER BY year) adds the years up to the current one.
A hint if you get stuck
Group by the hire year, then run a window SUM over the yearly counts, ordered by year.
Background you need
Common mistakes on this exercise
- No ORDER BY in the window: every year shows the grand total.
- Running total ordered by the number of hires, not by year.
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 1: headcount and payroll per department
- Payroll review, step 2: pay against the company average
- Payroll review, step 3: the top earner's share
- Payroll review, step 5: managers and their teams