Quick revision
The short answer
ROLLUP produces hierarchical subtotals from left to right, so ROLLUP (a, b, c) gives (a, b, c), (a, b), (a) and the grand total (), which is n + 1 groupings. CUBE produces every subset of the columns, 2^n groupings, so 8 for three columns. Each grouping set is its own aggregation, which can mean several sorts and a result that multiplies in size, so GROUPING SETS is the better tool when a report needs only some combinations.
What to remember
In this answer, we saw that ROLLUP and CUBE are shorthands for lists of grouping sets. ROLLUP (a, b, c) builds hierarchical subtotals, (a, b, c), (a, b), (a) and (), which is n + 1 sets, and one sort order can serve them all. CUBE builds every subset, 2^n sets, which is 8 for three columns and 1,024 for ten. Each set is a separate aggregation that may need its own sort or hash table, and the output multiplies, so we use GROUPING SETS to ask for exactly the combinations a report needs.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
ROLLUP produces hierarchical subtotals from left to right, so ROLLUP (a, b, c) gives (a, b, c), (a, b), (a) and the grand total (), which is n + 1 groupings. CUBE produces every subset of the columns, 2^n groupings, so 8 for three columns. Each grouping set is its own aggregation, which can mean several sorts and a result that multiplies in size, so GROUPING SETS is the better tool when a report needs only some combinations.
Why this question is asked
Reports almost always need totals at more than one level: sales per region and product, per region, and overall. Writing one query per level and gluing them with UNION ALL works, but SQL has grouping extensions that do it in one statement. Interviewers ask this concept question because the difference between ROLLUP and CUBE is easy to state and easy to misuse, and because the count of combinations explains why a harmless-looking CUBE can bring a database to its knees. In this answer, we will recap grouping sets, see exactly what ROLLUP and CUBE expand to, count the combinations, explain the cost, and show how GROUPING SETS asks for only what we need.
1A Quick Recap of Grouping Sets
A normal GROUP BY a, b produces one group per distinct pair of a and b. A grouping set is one such list of columns to group by, and GROUP BY GROUPING SETS lets us give several lists in one query. The database computes the aggregation for each list and stacks the results, filling the columns that were not part of that list with NULL. The empty set () means no grouping columns at all, which is the grand total.
ROLLUP and CUBE are shorthands that expand into a list of grouping sets. Knowing exactly what they expand to is the whole answer to this question.
2What ROLLUP Expands To
ROLLUP treats its columns as a hierarchy, such as year, month, day or country, region, city. It removes columns one at a time from the right, so each level is a subtotal of the level before it. For n columns that is n + 1 grouping sets.
Notice that the order of the columns matters. ROLLUP (country, city) gives totals per country, which makes sense, while ROLLUP (city, country) would give totals per city and skip the per-country level. ROLLUP fits drill-down reports where the hierarchy is natural.
3What CUBE Expands To
CUBE has no hierarchy. It produces every combination of the columns, every subset, including each column alone and the empty set. For n columns there are 2^n subsets, because each column is either in a set or not.
| Grouping set | ROLLUP (a, b, c) | CUBE (a, b, c) |
|---|---|---|
| (a, b, c) | yes | yes |
| (a, b) | yes | yes |
| (a, c) | no | yes |
| (b, c) | no | yes |
| (a) | yes | yes |
| (b) | no | yes |
| (c) | no | yes |
| () grand total | yes | yes |
So for three columns, CUBE gives 8 groupings where ROLLUP gives 4. CUBE suits cross-tab style analysis, where an analyst wants totals by any combination of dimensions, such as region and channel without product.
4Why CUBE Can Be Expensive
Each grouping set is a separate aggregation over the input. The database can share some work, but not all of it. With sort-based grouping, a ROLLUP is cheap because all its sets are prefixes of one sort order: sorted by (a, b, c), the data is also grouped correctly for (a, b) and (a), so one sort serves every level. CUBE's sets cannot all be prefixes of one order: (b, c) and (c) need a different sort from (a, b, c). The engine may sort the data several times, or build several hash tables in memory at once; PostgreSQL can mix both strategies, but each set still costs work over the whole input.
The output grows too. If a, b and c have 50, 200 and 30 distinct values, the (a, b, c) level alone can have up to 300,000 rows, and CUBE adds every other combination on top. The growth in sets is exponential: 4 columns give 16 sets, 6 columns give 64, and 10 columns give 1,024. A CUBE over many dimensions on a large table can run for a long time and return far more rows than anyone will read.
5Asking Only for What the Report Needs
Most reports do not need every combination. If a dashboard shows totals per region and product, per region, per product and overall, that is four sets, and GROUPING SETS lists exactly those four. We get the same rows CUBE would give for those levels, without paying for the rest.
So the practical rule is: ROLLUP for a hierarchy, GROUPING SETS for a specific list of levels, and CUBE only when the analysis truly needs every combination of a small number of columns.
6Follow-up questions
How do we tell a subtotal row from a row where the column is really NULL?
With the GROUPING() function. GROUPING(product) returns 1 when product was aggregated away in that row, meaning it is a subtotal, and 0 when product was a grouping column, even if its value is NULL.
Can we combine ROLLUP with normal grouping columns?
Yes. GROUP BY year, ROLLUP (region, product) keeps year in every set and rolls up the others, giving (year, region, product), (year, region) and (year). Every set includes year.
Does MySQL support CUBE?
MySQL supports ROLLUP with the WITH ROLLUP syntax and the GROUPING() function since 8.0, but not CUBE or GROUPING SETS, so check your version. In MySQL we combine queries with UNION ALL for the extra sets.
7Conclusion
In this answer, we saw that ROLLUP and CUBE are shorthands for lists of grouping sets. ROLLUP (a, b, c) builds hierarchical subtotals, (a, b, c), (a, b), (a) and (), which is n + 1 sets, and one sort order can serve them all. CUBE builds every subset, 2^n sets, which is 8 for three columns and 1,024 for ten. Each set is a separate aggregation that may need its own sort or hash table, and the output multiplies, so we use GROUPING SETS to ask for exactly the combinations a report needs.
Open with
Both are shorthands for grouping sets. ROLLUP treats the columns as a hierarchy and removes them from the right, so ROLLUP of a, b, c gives a b c, then a b, then a, then the grand total.
Then, if they want more
That is n plus one groupings, four for three columns, and it fits drill-down reports like country, region, city, where order matters. CUBE has no hierarchy and gives every subset of the columns, so two to the n groupings: eight for three columns, including combinations like b and c without a, and each column alone.
The cost comes from the fact that each grouping set is its own aggregation. A ROLLUP can share one sort, because all its sets are prefixes of the same order, but CUBE's sets need different orders, so the engine sorts several times or keeps several hash tables, and the output size multiplies.
It also grows exponentially: ten columns would be 1,024 groupings. So in practice I use ROLLUP for hierarchies, and GROUPING SETS when the report needs specific levels, for example region and product, region, product and the total, which is four sets instead of CUBE's eight.
I keep CUBE for a small number of dimensions when an analyst really needs every combination.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, The SQL Language
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is the difference between ROLLUP and CUBE? For GROUP BY CUBE (a, b, c), how many...": https://scrivio-interview-library.netlify.app/databases/advanced-sql/1fe887528a/