๐ Find the department with the highest total salary expense
Table:
Employees
Columns:
employee_id , employee_name , department_id , salary
๐ Query:
WITH dept_salary AS (
SELECT department_id,
SUM(salary) AS total_salary
FROM Employees
GROUP BY department_id
)
SELECT department_id,
total_salary
FROM dept_salary
WHERE total_salary = (
SELECT MAX(total_salary)
FROM dept_salary
);
๐ฏ Why this question matters:
โ Tests CTE + aggregation concepts
โ Evaluates nested subquery understanding
๐ Pro Tip:
Using a CTE first makes complex aggregate queries much cleaner and easier to debug.
๐ฅ React โค๏ธ for more advanced SQL interview questions ๐