Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 27/ 160
02. Advanced SQL & Relational Databases

Aggregations, GROUPING SETS, ROLLUP & CUBE

Produce multi-level aggregates in one pass with GROUPING SETS, ROLLUP, and CUBE.

AdvancedGROUPING SETS · ROLLUP · CUBE3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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 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.

Visual diagram

Loading interactive 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.

Aggregations, GROUPING SETS, ROLLUP & CUBE in one concrete design

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.

Key takeaway: GROUPING SETS requests multiple aggregation grains in one statement

Aggregations, 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.

Key takeaway: Enumerate only needed sets, use sentinel labels derived from GROUPING(column), and pre-aggregate at a sensible base grain before materializing report-level totals.

How it works, step by step

Step 1 of 4
GROUPING SETS

1. 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.

one row before grouping
Key numbers at this step
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 GROUPING marks 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

GROUPING SETS
Best for: An explicit list of grouping dimension tuples evaluated in a single aggregation statement.

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.

ROLLUP
Best for: A compact declaration of all hierarchical prefixes, such as (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.

CUBE
Best for: All combinations of listed dimensions, whose grouping count grows exponentially.

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 GROUPING marks 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, with GROUPING(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 GROUPING marks synthetic rows.
  • When is GROUPING SETS insufficient for CUBE groups, and what does adding ROLLUP cost in ROLLUP groups?
Interview tip: Lead with the data path and failure boundary: 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

Loading quiz

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.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
Analytical Window Frames (ROWS BETWEEN, RANGE BETWEEN, LAG, LEAD)
Next topic
Working with Semi-Structured Data in SQL (JSONB, Arrays, UNNEST)