Senior Aggregation SQL & Databases

Write a query using GROUPING SETS / ROLLUP.

Short answer: ROLLUP creates subtotals and a grand total. GROUPING SETS lists exact aggregation levels. GROUPING(col) detects total rows (NULLs that mean “all”).

Sample solution

T-SQL
SELECT DepartmentId, JobTitle, SUM(Salary) AS TotalSalary
FROM Employees
GROUP BY ROLLUP (DepartmentId, JobTitle);
Use GROUPING() to distinguish real NULL dimension values from rollup NULLs.
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details