Full answer
A sales report often wants detail rows, a subtotal per country and a grand total. ROLLUP produces all three in one query.
CREATE TABLE sales (country TEXT, product TEXT, amount INTEGER);
INSERT INTO sales VALUES ('India','Pen',10),('India','Bag',30),('India','Pen',5),('Spain','Pen',8),('Spain','Lamp',25);PostgreSQL:
SELECT country, product, SUM(amount) AS total
FROM sales
GROUP BY ROLLUP (country, product)
ORDER BY country NULLS LAST, product NULLS LAST;| country | product | total |
|---|---|---|
| India | Bag | 30 |
| India | Pen | 15 |
| India | NULL | 45 |
| Spain | Lamp | 25 |
| Spain | Pen | 8 |
| Spain | NULL | 33 |
| NULL | NULL | 78 |
Rows with product NULL are the country subtotals, and the row with both NULL is the grand total. GROUPING(col) returns 1 when the NULL was produced by the rollup, which matters if the data itself can contain NULLs:
SELECT country, product, GROUPING(country) AS g_country, GROUPING(product) AS g_product, SUM(amount) AS total
FROM sales
GROUP BY ROLLUP (country, product)
ORDER BY country NULLS LAST, product NULLS LAST;| country | product | g_country | g_product | total |
|---|---|---|---|---|
| India | Bag | 0 | 0 | 30 |
| India | Pen | 0 | 0 | 15 |
| India | NULL | 0 | 1 | 45 |
| Spain | Lamp | 0 | 0 | 25 |
| Spain | Pen | 0 | 0 | 8 |
| Spain | NULL | 0 | 1 | 33 |
| NULL | NULL | 1 | 1 | 78 |
SQLite has no ROLLUP:
SELECT country, product, SUM(amount) FROM sales GROUP BY ROLLUP (country, product); -- error on purposeError: no such function: ROLLUPThe emulation is a UNION ALL of the three levels:
SELECT country, product, total FROM (
SELECT country, product, SUM(amount) AS total, 0 AS lvl FROM sales GROUP BY country, product
UNION ALL
SELECT country, NULL, SUM(amount), 1 FROM sales GROUP BY country
UNION ALL
SELECT NULL, NULL, SUM(amount), 2 FROM sales
)
ORDER BY lvl = 2, country, lvl, product;| country | product | total |
|---|---|---|
| India | Bag | 30 |
| India | Pen | 15 |
| India | NULL | 45 |
| Spain | Lamp | 25 |
| Spain | Pen | 8 |
| Spain | NULL | 33 |
| NULL | NULL | 78 |
The extra lvl column keeps the subtotal under its details and the total at the end. Related tools: GROUPING SETS ((country), (product), ()) lets you pick any combination of groupings, and CUBE (a, b) produces every combination. Each grouping level costs another aggregation pass in the emulation, so on large tables a single ROLLUP query on an engine that supports it is cheaper.
CREATE TABLE sales (country TEXT, product TEXT, amount INTEGER);
INSERT INTO sales VALUES ('India','Pen',10),('India','Bag',30),('India','Pen',5),('Spain','Pen',8),('Spain','Lamp',25);
-- error on purpose
SELECT country, product, SUM(amount) FROM sales GROUP BY ROLLUP (country, product);
SELECT country, product, total FROM (
SELECT country, product, SUM(amount) AS total, 0 AS lvl FROM sales GROUP BY country, product
UNION ALL
SELECT country, NULL, SUM(amount), 1 FROM sales GROUP BY country
UNION ALL
SELECT NULL, NULL, SUM(amount), 2 FROM sales
)
ORDER BY lvl = 2, country, lvl, product;Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.
Key points
- ROLLUP adds subtotal and grand-total rows
- Rolled-up columns appear as NULL; GROUPING() identifies them
- SQLite lacks ROLLUP; emulate with UNION ALL of each level
- GROUPING SETS and CUBE generalise the idea
Common mistakes
- Confusing a subtotal NULL with a real NULL in the data
- Sorting so subtotal rows appear above their details
Follow-ups
- How would you label subtotal rows as 'All products'?
- What does CUBE (country, product) add on top of ROLLUP?
Tips
- Show how you sort the result so subtotals sit under their group