Aggregations, GROUPING SETS, ROLLUP & CUBE
Produce multi-level aggregates in one pass with GROUPING SETS, ROLLUP, and CUBE.
In plain words
One row before grouping. Next, detail and useful subtotals. Finally, avoid double-counting synthetic levels.
Think of it like…
ROLLUP is zooming out a folder tree one level at a time; CUBE asks for every shelf combination, so its cost grows like every checkbox subset.
Where you’ve seen it
GROUP BY GROUPING SETS ((region, channel, day), (region, day), (day), ()) produces detail, regional-day, day, and grand-total rows, with GROUPING(region) identifying subtotal rows.
Words you’ll hear
- GROUPING SETS
- An explicit list of grouping dimension tuples evaluated in a single aggregation statement.
- ROLLUP
- A compact declaration of all hierarchical prefixes, such as
(year,quarter,month),(year,quarter),(year), and(). - CUBE
- All combinations of listed dimensions, whose grouping count grows exponentially.
- Operational boundary
- A CUBE over many dimensions doubles output groups and can overwhelm a report, while NULL dimension values are indistinguishable from subtotal placeholders unless
GROUPINGmarks synthetic rows. Enumerate only needed sets, use sentinel labels derived fromGROUPING(column), and pre-aggregate at a sensible base grain before materializing report-level totals.
Visual diagram
Theory & deep dive
GROUPING SETS: the working mechanism
GROUPING SETS requests multiple aggregation grains in one statement; ROLLUP emits hierarchical prefixes, and CUBE emits every combination of the listed dimensions.
One row before grouping. Detail and useful subtotals, then GROUPING distinguishes placeholders; this exposes the state governed by CUBE.
GROUP BY GROUPING SETS ((region, channel, day), (region, day), (day), ()) produces detail, regional-day, day, and grand-total rows, with GROUPING(region) identifying subtotal rows.
GROUPING SETS requests multiple aggregation grains in one statementAggregations, GROUPING SETS, ROLLUP & CUBE: failure mode and production response
A CUBE over many dimensions doubles output groups and can overwhelm a report, while NULL dimension values are indistinguishable from subtotal placeholders unless GROUPING marks synthetic rows.
Enumerate only needed sets, use sentinel labels derived from GROUPING(column), and pre-aggregate at a sensible base grain before materializing report-level totals. Track ROLLUP groups as "N dimensions plus grand total: N+1", and make CUBE groups visible to the dataset owner.
GROUPING(column), and pre-aggregate at a sensible base grain before materializing report-level totals.How it works, step by step
Step 1 of 41. Define base grain
One row before grouping. GROUPING SETS requests multiple aggregation grains in one statement; ROLLUP emits hierarchical prefixes, and CUBE emits every combination of the listed dimensions.
- ROLLUP groups
- N dimensions plus grand total: N+1
- ROLLUP
- A compact declaration of all hierarchical prefixes, such as `(year,quarter,month)`, `(year,quarter)`, `(year)`, and `()`.
When to use it & trade-offs
What it is
GROUPING SETS requests multiple aggregation grains in one statement; ROLLUP emits hierarchical prefixes, and CUBE emits every combination of the listed dimensions. GROUP BY GROUPING SETS ((region, channel, day), (region, day), (day), ()) produces detail, regional-day, day, and grand-total rows, with GROUPING(region) identifying subtotal rows.
Why it matters: A CUBE over many dimensions doubles output groups and can overwhelm a report, while NULL dimension values are indistinguishable from subtotal placeholders unless GROUPING marks synthetic rows. Enumerate only needed sets, use sentinel labels derived from GROUPING(column), and pre-aggregate at a sensible base grain before materializing report-level totals.
When to reach for it
- Produce multi-level aggregates in one pass with GROUPING SETS, ROLLUP, and CUBE.
- Choose GROUPING SETS because it provides this capability: An explicit list of grouping dimension tuples evaluated in a single aggregation statement.
- Keep evidence for ROLLUP groups at "N dimensions plus grand total: N+1" so the last production stage-Validate totals - avoid double-counting synthetic levels-remains testable.
When to avoid or be careful
- A CUBE over many dimensions doubles output groups and can overwhelm a report, while NULL dimension values are indistinguishable from subtotal placeholders unless
GROUPINGmarks synthetic rows. - Do not conflate ROLLUP with GROUPING SETS. The second concept contributes different behavior: A compact declaration of all hierarchical prefixes, such as
(year,quarter,month),(year,quarter),(year), and(). - Release only after this guardrail is in place: Enumerate only needed sets, use sentinel labels derived from
GROUPING(column), and pre-aggregate at a sensible base grain before materializing report-level totals.
GROUPING SETS / ROLLUP / CUBE - roles, limits, and operating cost
Strengths: An explicit list of grouping dimension tuples evaluated in a single aggregation statement. GROUPING SETS requests multiple aggregation grains in one statement.
Watch out: A CUBE over many dimensions doubles output groups and can overwhelm a report, while NULL dimension values are indistinguishable from subtotal placeholders unless GROUPING marks synthetic rows.
(year,quarter,month), (year,quarter), (year), and ().Strengths: A compact declaration of all hierarchical prefixes, such as (year,quarter,month), (year,quarter), (year), and (). In this workflow it supports: Mark subtotal rows - GROUPING distinguishes placeholders.
Watch out: The operating cost is real: up to 2^N dimension combinations.
Strengths: All combinations of listed dimensions, whose grouping count grows exponentially. Its output can be checked with Grouping flag.
Watch out: Operating guardrail: Enumerate only needed sets, use sentinel labels derived from GROUPING(column), and pre-aggregate at a sensible base grain before materializing report-level totals.
Interview signals & numbers to know
When this topic comes up
- “Where is the failure boundary for GROUPING SETS, and how would ROLLUP groups reveal it?”
- “Why is ROLLUP distinct from this behavior: all combinations of listed dimensions, whose grouping count grows exponentially?”
- Production clue: A CUBE over many dimensions doubles output groups and can overwhelm a report, while NULL dimension values are indistinguishable from subtotal placeholders unless GROUPING marks synthetic rows.
- Design probe: Enumerate only needed sets, use sentinel labels derived from GROUPING(column), and pre-aggregate at a sensible base grain before materializing report-level totals.
Benchmarks & rules of thumb
- ROLLUP groups
- N dimensions plus grand total: N+1
- CUBE groups
- up to 2^N dimension combinations
- Grouping flag
- 1 marks a synthetic subtotal for that dimension
Common mistakes & follow-ups
Common interview mistakes
- A CUBE over many dimensions doubles output groups and can overwhelm a report, while NULL dimension values are indistinguishable from subtotal placeholders unless
GROUPINGmarks synthetic rows. - Reporting success from a job exit code without checking ROLLUP groups and CUBE groups.
- Treating this example as complete without replay, rollback, or access boundaries:
GROUP BY GROUPING SETS ((region, channel, day), (region, day), (day), ())produces detail, regional-day, day, and grand-total rows, withGROUPING(region)identifying subtotal rows.
Likely interviewer follow-ups
- How would you prove this contract instead of only checking that the command ran: An explicit list of grouping dimension tuples evaluated in a single aggregation statement.
- What does the recovery plan do when this occurs? A CUBE over many dimensions doubles output groups and can overwhelm a report, while NULL dimension values are indistinguishable from subtotal placeholders unless
GROUPINGmarks synthetic rows. - When is GROUPING SETS insufficient for CUBE groups, and what does adding ROLLUP cost in ROLLUP groups?
GROUPING SETS requests multiple aggregation grains in one statement; ROLLUP emits hierarchical prefixes, and CUBE emits every combination of the listed dimensions. Then make the answer operational: Enumerate only needed sets, use sentinel labels derived from GROUPING(column), and pre-aggregate at a sensible base grain before materializing report-level totals.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Aggregations, GROUPING SETS, ROLLUP & CUBE with data engineer interview Q&A and hands-on practice.