Skip to content

ROLLUP

SELECT Country, State, COUNT(Name)
FROM Customers
GROUP BY Country, State WITH ROLLUP;
| Country | State | COUNT(Name) |
|----------|---------|---------------|
| USA | TX | 100 |
| USA | CA | 120 |
| USA | NULL | 220 |
| Canada | BC | 150 |
| Canada | NULL | 150 |
| NULL | NULL | 370 |

In the example, the ROLLUP operator is used to produce a result set that is similar to the one generated by the GROUP BY clause. However, with ROLLUP, extra rows are added to calculate the subtotals, which are grouped by Country and State. Also, a grand total row is added to the result set.