SQL Grouping Sets
Learn how to produce multiple levels of aggregation in a single query using GROUPING SETS, ROLLUP, and CUBE.
What Is Aggregation?
When you use GROUP BY in SQL, you collapse many rows into summary rows. Functions like SUM, COUNT, and AVG compute a single value for each group.
But what if you need both levels in the same result: totals per region and totals per region-product? That is exactly the problem GROUPING SETS solves.
The Multi-Level Problem
Without GROUPING SETS, you would write separate queries for each grouping level and combine them with UNION ALL:
Introducing GROUPING SETS
GROUPING SETS is a clause inside GROUP BY that lets you list multiple grouping combinations. The database computes all of them in one pass over the data.
How It Works Visually
The Formal Syntax
ROLLUP vs CUBE vs GROUPING SETS
| Feature | GROUPING SETS | ROLLUP | CUBE |
|---|---|---|---|
| You choose sets | ✓ Explicit | Automatic hierarchy | Automatic all combos |
| For (a, b) | You list each set | (a,b), (a), () | (a,b), (a), (b), () |
| Number of sets | Exactly what you list | \( n + 1 \) | \( 2^n \) |
| Best for | Custom combos | Hierarchical totals | Cross-tab reports |
Shortcut: ROLLUP
ROLLUP(a, b) is equivalent to writing out a hierarchical grouping sets list. It peels off columns right to left:
Shortcut: CUBE
CUBE(a, b) generates every possible combination of the listed columns, including the empty set:
The GROUPING() Function
When a column is NULL, how do you tell the difference between a real NULL in the data and a grouping-set NULL? Use the GROUPING() function.
How to Apply Grouping Sets
Example 1: Sales Report
| region | product | total |
|---|---|---|
| East | Gadget | 200 |
| East | Widget | 100 |
| East | NULL | 300 |
| West | Gadget | 250 |
| West | Widget | 150 |
| West | NULL | 400 |
| NULL | NULL | 700 |
Example 2: ROLLUP for Dates
This produces four aggregation levels automatically:
Example 3: CUBE Cross-Tab
| dept | gender | avg_sal |
|---|---|---|
| Eng | F | 95000 |
| Eng | M | 92000 |
| Eng | NULL | 93500 |
| Sales | F | 78000 |
| Sales | M | 81000 |
| Sales | NULL | 79500 |
| NULL | F | 86500 |
| NULL | M | 86500 |
| NULL | NULL | 86500 |
Common Mistakes
Summary at a Glance
| Concept | Key Idea | When to Use |
|---|---|---|
| GROUPING SETS | List exact combos you want | Custom reporting needs |
| ROLLUP | Hierarchical: peels right to left | Date or geographic hierarchies |
| CUBE | All \( 2^n \) combinations | Cross-tab / pivot reports |
| GROUPING() | Returns 1 if NULL from aggregation | Disambiguating NULL values |
| Empty set () | Grand total row | Overall summary line |
Question 1 of 5
What does GROUPING SETS allow you to do in a single query?
Question 2 of 5
Which of the following is equivalent to GROUP BY ROLLUP(a, b)?
Question 3 of 5
How many grouping sets does CUBE(x, y, z) produce?
Question 4 of 5
What does GROUPING(column) return when the NULL in that column is caused by aggregation?
Question 5 of 5
What does the empty set () inside GROUPING SETS produce?