SQL business case: the payroll review
Finance is preparing next year's budget and asks for the people numbers: how many people each department has and what they cost, which departments pay above the company average, how concentrated the pay is, how the team grew, and how large each manager's team is.
Five exercises on the employees and departments tables. The traps are the everyday ones: the department with nobody in it, the employee without a department, two people on the same top salary.
Step 1: Headcount and payroll per department
- Payroll review, step 1: headcount and payroll per departmentFinance 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.
Step 2: Pay against the company average
- Payroll review, step 2: pay against the company averageWhich departments pay above the company average? For every department that has employees, return the department name, its average salary rounded to a whole number, and the difference between that average and the average salary of all employees, also rounded to a whole number. All employees means everyone, including those without a department.
Step 3: The top earner's share
- Payroll review, step 3: the top earner's shareHow concentrated is the pay? For each department, return the department name, the name of its highest-paid employee and that salary as a percentage of the department's total salary, rounded to 1 decimal. If several employees share the top salary, return each of them.
Step 4: Hires per year and running headcount
- Payroll review, step 4: hires per year and running headcountHow 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.
Step 5: Managers and their teams
- Payroll review, step 5: managers and their teamsClose 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.
Other plans and cases
- 14-day SQL interview sprint (41)
- SQL for data analyst interviews (24)
- SQL for BI analyst interviews (21)
- SQL for data engineer interviews (24)
- Product analytics SQL in four sessions (12)
- SQL traps and time periods in three sessions (10)
- Window functions in six sessions (18)
- SQL on messy data: one work week (16)
- PostgreSQL features in four sessions (11)
- SQL business case: the refund review (5)
- SQL business case: the monthly revenue report (4)
- SQL business case: customer value and segments (4)
- SQL business case: product performance (4)
- All exercises by level
Your first 8 exercises are free, with no account, from any plan or level. Progress is kept in this browser; a free account keeps it on every device.