Learn SQL
-- aggregations.rollup_cube

ROLLUP, CUBE & GROUPING SETS in SQL

Subtotals and grand totals in one scan

--concept

ROLLUP gives hierarchical totals (per-department rows plus a grand-total row); CUBE gives all column combinations; GROUPING SETS lists exact groupings wanted. NULLs in super-aggregate rows are ambiguous with real NULLs — identify them with GROUPING(). One pass with subtotals beats UNIONing multiple GROUP BYs.

--how it works
1

ROLLUP hierarchies

GROUP BY ROLLUP (dept) yields per-dept rows plus a grand-total row (NULL key).

2

CUBE all combos

Every column combination — powerful, expensive on wide keys.

3

GROUPING SETS exact

List precisely which groupings to emit — no more, no less.

4

GROUPING() disambiguates

1 means total-NULL, 0 means real-NULL. Labels depend on it.

--visualization
Hierarchical totals — one scan, GROUPING()=1 flags the total row
departmentn
Engineering3
Marketing1
Sales2
NULL (total)6
Stage 1 / 1
NULL department = grand total. GROUPING() tells total-NULL apart from real-NULL for labels.
--example
query.sql
1SELECT department, COUNT(*) AS n FROM employees GROUP BY ROLLUP (department);
Result
departmentn
Engineering3
Marketing1
Sales2
null6

Three per-department rows plus a fourth where department is NULL — the grand total of 6. GROUPING() tells the report renderer to label that row Total instead of blank.

--variants

Dept rollup + total

Three groups plus grand total in one scan.

query.sql
1SELECT department, COUNT(*) AS n FROM employees GROUP BY ROLLUP (department);
Result
departmentn
Engineering3
Marketing1
Sales2
null6
1First three rows are per-department.
2Fourth row NULL department = grand total 6.
3GROUPING(department) flags that row as total for the renderer.

Exact sets

Only the groupings you list.

query.sql
1SELECT department, COUNT(*) FROM employees GROUP BY GROUPING SETS ((department), ());
Result
departmentcount
Engineering3
Sales2
Marketing1
null6
1First set groups by department.
2Empty set () emits the grand total.
3No intermediate combos — unlike CUBE.
--key facts
ROLLUPhierarchical totals in one scan
CUBEevery combination — expensive on wide keys
GROUPING()total-NULL vs real-NULL detector
--use cases
Financial subtotals: region → store → total
Dashboard rollups without UNIONing GROUP BYs
--under the hood
Rollup executes as grouped aggregation with super-aggregate rows appended — one sort/hash, not many.
CUBE row explosion is exponential in keys — constrain with SETS.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.