Payroll review, step 5: managers and their teams
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
Close the review with the span of control. For every manager with at least two direct reports, return the manager's name, the number of direct reports, the total salary of those reports and their average salary rounded to a whole number.
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 self join gives the same table two roles. Grouping by the manager turns one row per report into one row per team, and HAVING keeps the teams of the size the question asks for.
A hint if you get stuck
Join employees to itself: e is the report, m is the manager. Group by the manager and filter the groups.
Background you need
Common mistakes on this exercise
- At least two means two counts as well.
- Adds up the manager's own salary once per report instead of the reports' salaries.
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 4: hires per year and running headcount