Department Budget vs Salary Cost
For each department, how does the total salary cost compare to the budget, and what is the average salary?
- Joins
- Aggregation
- Numeric functions
- NULL handling
- Sorting
Challenge brief
Understand the request
HR Finance is reviewing compensation spend against approved departmental budgets ahead of headcount planning.
Show department budget, actual salary cost, average salary, and remaining budget by joining employees to departments.
Return
- department_name
- budget
- head_count
- avg_salary (rounded 2)
- total_salary_cost
- remaining_budget (budget minus total salary)
Constraints
- Include every department, including departments with no employees
- Report zero salary cost and the full budget remaining for empty departments
- Show the largest budgets first
Data you will use
Review the relevant tables before deciding how to join, filter, or aggregate them.
departments
department_idINTEGERdepartment_nameVARCHAR(100)budgetINTEGERhead_countINTEGER
employees
employee_idINTEGERdepartment_idINTEGERsalaryINTEGER
Hints, when you need them
Open one clue at a time so you still do the reasoning.
Hint 1
Budget is in departments. Salaries are in employees. INNER JOIN on department_id. GROUP BY department. AVG and SUM the salaries. Subtract SUM from budget for remaining.
Hint 2
INNER JOIN departments to employees on department_id. GROUP BY d.department_id (plus all non-aggregated columns). ROUND(AVG(e.salary), 2), SUM(e.salary), d.budget - SUM(e.salary) AS remaining_budget.
Hint 3
Build question 15 from its business grain: identify the driving rows, add only valid relationships, then apply the required filtering, aggregation, and deterministic ordering.
Verified SQL answer
Attempt the problem first, then compare structure and reasoning—not just syntax.
Reveal solution and explanation
SELECT d.department_name, d.budget, d.head_count, ROUND(AVG(e.salary), 2) AS avg_salary, COALESCE(SUM(e.salary), 0) AS total_salary_cost, d.budget - COALESCE(SUM(e.salary), 0) AS remaining_budget FROM departments d LEFT JOIN employees e ON d.department_id = e.department_id GROUP BY d.department_id, d.department_name, d.budget, d.head_count ORDER BY d.budget DESC, d.department_id;Why this works
Drive from departments and retain missing employee matches. SUM is converted to zero for an empty department, while AVG remains null because no employee salary exists.
Success check
All 7 departments appear; Services has no employees, zero salary cost, null average salary, and its full $12 million budget remaining.
Expected result
Use this output to verify values, aliases, ordering, and row count.
| department_name | budget | head_count | avg_salary | total_salary_cost | remaining_budget |
|---|---|---|---|---|---|
| Software Engineering | 50000000 | 3 | 193333.33 | 580000 | 49420000 |
| Hardware Engineering | 40000000 | 2 | 235000 | 470000 | 39530000 |
| Retail | 25000000 | 2 | 157500 | 315000 | 24685000 |
| Design | 15000000 | 1 | 180000 | 180000 | 14820000 |
| Services | 12000000 | 0 | NULL | 0 | 12000000 |
| Executive | 10000000 | 1 | 300000 | 300000 | 9700000 |
| Environment | 8000000 | 1 | 200000 | 200000 | 7800000 |
Learn the concepts behind this answer
Strengthen your understanding with these targeted learning topics:
Continue practicing
Explore related company challenges
Microsoft
Independent Microsoft-style cloud, productivity, subscription, usage, support, and customer analytics SQL practice.
Google
Independent Google-style search, advertising, user-engagement, and video-product SQL practice.
Amazon
Independent Amazon-style e-commerce, warehouse, inventory, and customer analytics SQL practice.
Return to the complete interview preparation experience.