ROLLUP, CUBE and GROUPING SETS
subtotals and a grand total in one query, and GROUPING() to tell their NULLs from yours.
- address
- everything.analytics.grouping_sets
- part
- part 4, windows, grouping and recursion
- chapter
- chapter 2 of 11
- reading time
- 7 min
GROUPING SETS runs several GROUP BYs in one query and stacks the results: detail rows, subtotals and a grand total together. ROLLUP (a, b) is shorthand for the sets (a, b), (a) and (): a hierarchy of subtotals. CUBE (a, b) is every combination.
in a subtotal row, the columns that were rolled up come out NULL. GROUPING() tells those NULLs apart from NULLs that were in your data.
the picture
you know the spreadsheet report with a subtotal under every year and a grand total at the bottom? the SQL version used to be three queries glued with UNION ALL: one GROUP BY year, status, one GROUP BY year, one with no GROUP BY at all. ROLLUP is those three, in one query, in one pass over the table.
think of counting votes. first by polling station and party (the detail), then you add up each station's sheet (a subtotal per station), then everything (the grand total). same ballots each time; you just stop distinguishing one more column at each level.
- 1SELECT year, status, count(*) AS orders, GROUPING(year, status) AS g
- 2FROM (SELECT extract(year FROM ordered_at)::int AS year, status FROM orders) AS o
- 3GROUP BY year, status
- 4ORDER BY year NULLS LAST, status NULLS LAST;
| yearint4 | statustext | ordersint8 | gint4 |
|---|---|---|---|
| 2025 | cancelled | 183 | 0 |
| 2025 | delivered | 4864 | 0 |
| 2025 | refunded | 47 | 0 |
| 2026 | cancelled | 134 | 0 |
| 2026 | delivered | 4488 | 0 |
| 2026 | paid | 62 | 0 |
| 2026 | placed | 51 | 0 |
| 2026 | refunded | 51 | 0 |
| 2026 | shipped | 120 | 0 |
plain GROUP BY: one row per year and status that exists. 9 rows, and GROUPING() is 0 on every one: nothing is rolled up.
g is GROUPING(year, status): 0 for a detail row, more for a subtotal.look at ROLLUP's extra rows. 2025 | null | 5,094 is every order from 2025, whatever its status. null | null | 10,000 is every order. the NULL means "all of them" there, not "unknown". more on that collision below.
the three spellings
all three are ways to write a list of grouping sets, and postgres expands them before it runs anything:
- n + 1 sets
- ├── (year, status)the detail rows
- ├── (year)a subtotal per year
- └── ()the grand total
- 2^n sets
- ├── (year, status)the detail rows
- ├── (year)a subtotal per year
- ├── (status)a subtotal per status
- └── ()the grand total
- exactly what you list
- ├── (year)
- └── (status)
so this query and the UNION ALL of three GROUP BYs return the same rows (with NULL standing in for the columns a set doesn't group by):
SELECT year, status, count(*) AS orders
FROM (SELECT extract(year FROM ordered_at)::int AS year, status FROM orders) AS o
GROUP BY ROLLUP (year, status)
ORDER BY year NULLS LAST, status NULLS LAST;| yearint4 | statustext | ordersint8 |
|---|---|---|
| 2025 | cancelled | 183 |
| 2025 | delivered | 4864 |
| 2025 | refunded | 47 |
| 2025 | null | 5094 |
| 2026 | cancelled | 134 |
| 2026 | delivered | 4488 |
| 2026 | paid | 62 |
| 2026 | placed | 51 |
| 2026 | refunded | 51 |
| 2026 | shipped | 120 |
| 2026 | null | 4906 |
| null | null | 10000 |
column order matters for ROLLUP, since it's a hierarchy: ROLLUP (status, year) gives a subtotal per status instead. CUBE doesn't care about order. and you can mix: GROUP BY year, ROLLUP (status, kind) keeps year in every set and rolls up the rest.
SELECT a, b, count(*)
FROM (VALUES (1, 'x'), (1, 'y'), (2, 'x')) AS t (a, b)
GROUP BY CUBE (a, b);
-- how many rows come back besides the 3 detail rows?SELECT a, b, count(*) AS n, GROUPING(a, b) AS g
FROM (VALUES (1, 'x'), (1, 'y'), (2, 'x')) AS t (a, b)
GROUP BY CUBE (a, b)
ORDER BY g, a, b;| aint4 | btext | nint8 | gint4 |
|---|---|---|---|
| 1 | x | 1 | 0 |
| 1 | y | 1 | 0 |
| 2 | x | 1 | 0 |
| 1 | null | 2 | 1 |
| 2 | null | 1 | 1 |
| null | x | 2 | 2 |
| null | y | 1 | 2 |
| null | null | 3 | 3 |
GROUPING(): whose NULL is it?
the NULL in a subtotal row means "rolled up". but your data can hold NULLs of its own, and then the two look exactly alike. the shop has three products with no category:
- 1SELECT category, count(*) AS products
- 2FROM products
- 3GROUP BY ROLLUP (category)
- 4ORDER BY products, category;
| categorytext | productsint8 |
|---|---|
| null | 3 |
| books | 12 |
| games | 14 |
| garden | 15 |
| pets | 17 |
| kitchen | 19 |
| audio | 21 |
| outdoor | 22 |
| toys | 24 |
| office | 26 |
| bath | 27 |
| null | 200 |
ROLLUP over category: two rows say NULL. one is the three products that have no category, the other is the grand total. nothing in the output says which is which.
category, two rows say null: the three uncategorised products, and the grand total of 200. GROUPING(category) is 1 only on the rolled-up row, so it can label them.GROUPING(col) is 0 when col is part of the current row's grouping set, 1 when it was rolled up. with several arguments it packs those bits into a number, the first argument as the highest bit. for GROUPING(year, status): 0 is a detail row, 1 is a per-year subtotal (status rolled up), 2 a per-status subtotal, 3 the grand total. that number is also the cleanest way to sort a report: details first, then subtotals, then the total, or the other way round.
SELECT CASE GROUPING(year, status)
WHEN 0 THEN 'detail' WHEN 1 THEN 'year total'
WHEN 2 THEN 'status total' ELSE 'grand total' END AS kind,
year, status, count(*) AS orders
FROM (SELECT extract(year FROM ordered_at)::int AS year, status FROM orders) AS o
GROUP BY CUBE (year, status)
HAVING GROUPING(year, status) > 0
ORDER BY GROUPING(year, status), year, status;| kindtext | yearint4 | statustext | ordersint8 |
|---|---|---|---|
| year total | 2025 | null | 5094 |
| year total | 2026 | null | 4906 |
| status total | null | cancelled | 317 |
| status total | null | delivered | 9352 |
| status total | null | paid | 62 |
| status total | null | placed | 51 |
| status total | null | refunded | 98 |
| status total | null | shipped | 120 |
| grand total | null | null | 10000 |
HAVING works per grouping set, like it works per group: here it keeps only the subtotal rows.
one pass, not three
ROLLUP isn't a UNION ALL in disguise. postgres plans all the sets together, often in one sort or one hash pass:
EXPLAIN (COSTS OFF)
SELECT year, status, count(*)
FROM (SELECT extract(year FROM ordered_at)::int AS year, status FROM orders) AS o
GROUP BY ROLLUP (year, status);a MixedAggregate: one hash table per grouping set (Hash Key for (year, status), and for (year)), plus Group Key: (), the single group of the grand total. all three are filled from the same scan of orders. the three-query UNION ALL would read orders three times. (aggregation algorithms has hash vs sort aggregation.)
keylines
- GROUPING SETS runs several GROUP BYs in one query and stacks the results.
- ROLLUP (a, b) = (a, b), (a), (): a hierarchy of subtotals. order matters.
- CUBE (a, b) = every subset of the columns: 2ⁿ grouping sets.
- rolled-up columns come out NULL. GROUPING(col) = 1 tells them from your own NULLs.
- it's one pass over the data, not a UNION ALL of separate scans.