Quick revision
The short answer
SELECT region, product, SUM(amount) FROM sales GROUP BY ROLLUP (region, product) returns totals per region and product, per region, and a grand total in one pass; GROUPING SETS ((region, product), (region), ()) is the explicit equivalent. Subtotal rows have NULL in the rolled-up column, so we tell them apart from a genuinely NULL product with GROUPING(product), which returns 1 only when the column was aggregated away.
What to remember
In this answer, we wrote one query that returns totals per region and product, per region, and a grand total with GROUP BY ROLLUP (region, product), whose explicit form is GROUPING SETS ((region, product), (region), ()), while CUBE would add (product) alone. We saw that subtotal rows and real NULL products look the same, and that GROUPING(product) returns 1 only on rows where the column was aggregated away, which lets us label and order them correctly. MySQL offers WITH ROLLUP and GROUPING() since 8.0 but not CUBE or GROUPING SETS, and one ROLLUP pass is usually cheaper than several UNION ALL queries.
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.
SELECT region, product, SUM(amount) FROM sales GROUP BY ROLLUP (region, product) returns totals per region and product, per region, and a grand total in one pass; GROUPING SETS ((region, product), (region), ()) is the explicit equivalent. Subtotal rows have NULL in the rolled-up column, so we tell them apart from a genuinely NULL product with GROUPING(product), which returns 1 only when the column was aggregated away.
Why this question is asked
Almost every finance or sales report needs the same shape: detail lines, subtotals and a grand total. Writing it as three queries glued with UNION ALL is common, slower than it needs to be, and hides a subtle bug when the data itself contains NULLs. Interviewers ask this coding question to see whether we know the grouping extensions and, more importantly, whether we know the trap they carry. In this answer, we will recap what the query has to produce, write it with ROLLUP and with GROUPING SETS, reproduce the NULL ambiguity with real rows, fix it with GROUPING(), and look at how the same report is written in MySQL.
1A Quick Recap of the Report
We need three levels of totals from sales(region, product, amount): one row per region and product, one subtotal row per region, and one grand total row. Each level is a grouping set, a list of columns to group by. The three sets we need are (region, product), (region) and the empty set (), which groups everything together.
Let us use a small table with one awkward row: a sale in EU whose product is unknown, stored as NULL. Real data has such rows, from imports or deleted products.
2Writing It With ROLLUP
ROLLUP (region, product) expands to exactly our three sets, because it removes columns from the right one at a time: (region, product), then (region), then ().
The explicit form is GROUP BY GROUPING SETS ((region, product), (region), ()), and it returns the same rows. It is longer, but it shows exactly which levels we get, and it lets us add or remove levels freely. CUBE (region, product) would add one more set, (product) alone, giving totals per product across all regions, which this report does not need.
3The Problem: Two Kinds of NULL
Look at the two EU rows with NULL in product. One is the real sale of 3 with an unknown product, and the other is the EU subtotal of 18. The query marks a rolled-up column with NULL, so the two rows look the same. A report that labels every NULL product as "All products" would show two "EU, All products" lines, one of them wrong, and a program that picks subtotal rows with WHERE product IS NULL would also pick the real sale.
Notice that we cannot fix this by looking at values. The total 3 is not obviously a detail row, and in other data a subtotal could be any number. We need the query itself to tell us which grouping set produced each row.
4Fixing It With GROUPING()
The GROUPING() function answers exactly that. GROUPING(product) returns 1 when product was not part of the grouping set for that row, meaning the row is a subtotal over all products, and 0 when product was a grouping column, even if its value happens to be NULL. With several arguments, GROUPING(region, product) returns a bit mask: 0 for detail rows, 1 for region subtotals and 3 for the grand total.
| Row | GROUPING(product) | GROUPING(region, product) |
|---|---|---|
| EU, A, 10 | 0 | 0 |
| EU, NULL, 3 (real NULL) | 0 | 0 |
| EU, NULL, 18 (subtotal) | 1 | 1 |
| NULL, NULL, 25 (grand total) | 1 | 3 |
Now the real NULL is labelled "(unknown)" and the subtotal "All products". We also order by the GROUPING() values, so each subtotal comes right after its details and the grand total comes last; without an ORDER BY, the database does not promise any order. A Java client can read the level column and style subtotal rows without guessing.
5The Same Report in MySQL
MySQL supports ROLLUP with its own syntax, WITH ROLLUP after the GROUP BY list, and has had the GROUPING() function since 8.0. It does not support the CUBE or GROUPING SETS syntax, so check your version if you need them; for other combinations we would add a UNION ALL branch.
Why not simply write three queries with UNION ALL? Because each branch reads the whole sales table again. One ROLLUP query reads it once and computes all three levels from one sort, which on a large table is usually several times cheaper, and the query is shorter to read and change, since adding a level means adding one grouping set rather than another branch.
6Follow-up questions
What does CUBE (region, product) add here?
A fourth set, (product) alone, so we also get a total per product across all regions, such as A = 17. CUBE gives every combination, 2^n sets for n columns.
Could we use COALESCE(product, 'All products') instead of GROUPING()?
Only if product can never be NULL in the data. If it can, COALESCE turns the real unknown-product sale into a fake subtotal line. GROUPING() is the only reliable way to tell the two apart.
How do we keep only subtotal rows?
Filter on GROUPING() in a HAVING clause or an outer query, for example HAVING GROUPING(product) = 1. Filtering on product IS NULL would also return the real NULL rows.
7Conclusion
In this answer, we wrote one query that returns totals per region and product, per region, and a grand total with GROUP BY ROLLUP (region, product), whose explicit form is GROUPING SETS ((region, product), (region), ()), while CUBE would add (product) alone. We saw that subtotal rows and real NULL products look the same, and that GROUPING(product) returns 1 only on rows where the column was aggregated away, which lets us label and order them correctly. MySQL offers WITH ROLLUP and GROUPING() since 8.0 but not CUBE or GROUPING SETS, and one ROLLUP pass is usually cheaper than several UNION ALL queries.
Open with
I would write select region, product, sum of amount from sales group by rollup of region and product. Rollup removes columns from the right, so it produces the three grouping sets we need: region and product, region alone, and the empty set for the grand total.
Then, if they want more
The explicit equivalent is grouping sets with those three lists, and cube would add product alone as a fourth set. The tricky part is NULL.
In the subtotal rows, the rolled-up column is shown as NULL, so if the data has a sale with an unknown product, that row and the region subtotal both show NULL in product and look the same. Coalesce cannot fix that, because it would relabel the real NULL too.
The fix is the GROUPING function: grouping of product returns one when product was aggregated away in that row, and zero when it was a grouping column, even if its value is NULL.
So I label with a case on grouping, show the real NULL as unknown, and order by the grouping values so subtotals follow their details and the total comes last. In MySQL the syntax is group by region, product with rollup, and grouping is available since 8.0, but cube and grouping sets are not.
And one rollup pass reads the table once, which is usually cheaper than three queries combined with union all.
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 "Write one query over sales(region, product, amount) that returns totals per region and...": https://scrivio-interview-library.netlify.app/databases/advanced-sql/6f8742b0ed/