Why are GROUPING SETS, ROLLUP and CUBE often more efficient than UNION ALL of separate GROUP BY queries?
Quick Overview
Asks why SQL grouping extensions such as GROUPING SETS, ROLLUP and CUBE usually run faster than a UNION ALL of separate GROUP BY queries. Tests understanding of query plans, scan counts, which aggregates can be rolled up from subtotals, and how subtotal rows are labeled.
Why are GROUPING SETS, ROLLUP and CUBE often more efficient than UNION ALL of separate GROUP BY queries?
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
A report needs the same measure at several levels of detail, for example sales by region and product, by region alone, and a grand total. Why is using `GROUPING SETS`, `ROLLUP`, or `CUBE` often more efficient than achieving the same aggregations with `UNION ALL` and separate `GROUP BY` clauses?
```hint Count the passes
Picture the query plan for each version. How many times does each one read the base table, and how many times does it sort or hash those rows?
```
```hint Not every aggregate rolls up
Ask whether a region total can be computed from the region-and-product totals. Does the answer change for `COUNT(DISTINCT ...)`?
```
### What a Strong Answer Covers
- How many times the base table is read and aggregated in each approach
- How one sort or hash pass can produce several grouping levels at once
- Which aggregates can be built from finer subtotals and which still need the base rows
- The readability and correctness gains of one statement over several near-copies, including how subtotal rows are labeled
- Cases where the advantage shrinks or where separate queries can win
### Follow-up Questions
- How do you tell the NULL in a subtotal row apart from a NULL that is really in the data?
- The report needs `COUNT(DISTINCT user_id)` at every level. Does the efficiency argument still hold?
- When could a `UNION ALL` of separate queries be faster than `GROUPING SETS`?
- How many grouping sets does `CUBE` over four columns produce, and how would you avoid computing the ones you don't need?
Overview: Asks why SQL grouping extensions such as GROUPING SETS, ROLLUP and CUBE usually run faster than a UNION ALL of separate GROUP BY queries. Tests understanding of query plans, scan counts, which aggregates can be rolled up from subtotals, and how subtotal rows are labeled.
A report needs the same measure at several levels of detail, for example sales by region and product, by region alone, and a grand total. Why is using GROUPING SETS, ROLLUP, or CUBE often more efficient than achieving the same aggregations with UNION ALL and separate GROUP BY clauses?
What a Strong Answer Covers Guidance
How many times the base table is read and aggregated in each approach
How one sort or hash pass can produce several grouping levels at once
Which aggregates can be built from finer subtotals and which still need the base rows
The readability and correctness gains of one statement over several near-copies, including how subtotal rows are labeled
Cases where the advantage shrinks or where separate queries can win
Follow-up Questions Guidance
How do you tell the NULL in a subtotal row apart from a NULL that is really in the data?
The report needs
COUNT(DISTINCT user_id)
at every level. Does the efficiency argument still hold?
When could a
UNION ALL
of separate queries be faster than
GROUPING SETS
?
How many grouping sets does
CUBE
over four columns produce, and how would you avoid computing the ones you don't need?