Stop 05 · Aggregation & GroupingTopics · 7/9

ROLLUP and GROUPING SETS

Part of the SQL Roadmap

In short

Finance teams love tables with subtotals under each region and a grand total at the bottom. ROLLUP builds all of those levels in one query, so you stop gluing results together by hand.

ROLLUP and GROUPING SETS add subtotal and grand-total rows to a grouped result in one query. PostgreSQL and SQL Server support both; MySQL supports WITH ROLLUP.

The jaadu stacks steel catering trays into small towers by course, then lifts all the towers onto one giant brass platter.

Think of it like this

The caterer's wedding invoice

A caterer's invoice lists every dish, then a subtotal after each course (starters, mains, desserts), then one grand total at the end. Nobody adds the subtotals by hand later. ROLLUP prints the dish lines, the course subtotals and the grand total together, in one go.

Watch it happen

Revenue by region and category, with subtotals

All regions ₹19L
North ₹11L
South ₹8L
Electronics ₹7L
Fashion ₹4L
Electronics ₹5L
Fashion ₹3L

1/4A normal GROUP BY region, category gives these four detail rows.

The big ideas

What you need to know

ROLLUP is a hierarchy

ROLLUP (region, category) gives (region, category), then (region), then (). It rolls up from right to left.

GROUPING SETS picks levels

GROUPING SETS ((region), (category), ()) gives exactly the groupings you list, nothing more.

Label the NULLs

Subtotal rows show NULL in rolled-up columns. GROUPING(col) returns 1 on those rows so you can label them 'All'.

Database support

PostgreSQL and SQL Server support ROLLUP, CUBE and GROUPING SETS. MySQL uses GROUP BY ... WITH ROLLUP.

See it in code

Walk through the code

sql
1SELECT2  CASE WHEN GROUPING(region) = 1 THEN 'All regions'3       ELSE region END                  AS region_label,4  CASE WHEN GROUPING(category) = 1 THEN 'All categories'5       ELSE category END                AS category_label,6  SUM(amount)                           AS revenue7FROM sales8WHERE sale_date >= DATE '2026-04-01'9GROUP BY ROLLUP (region, category)10ORDER BY GROUPING(region), region,11         GROUPING(category), category;

1/3GROUPING() is 1 on rows where that column was rolled up, so we swap NULL for a clear label.

Revenue for this financial year with region subtotals and a grand total. In MySQL 8, write GROUP BY region, category WITH ROLLUP instead.

Save yourself the pain

Common mistakes

Oops

Using COALESCE(region, 'All') to label subtotal rows.

Do this instead

If region itself has real NULLs, those get mislabelled too. GROUPING(region) = 1 only marks true subtotal rows.

Oops

Expecting ROLLUP (region, category) to also give a total per category across regions.

Do this instead

ROLLUP only goes up one hierarchy. Use CUBE or GROUPING SETS ((region), (category), ()) for every combination.

Oops

Adding SUM(revenue) over all rows of the result to get the total.

Do this instead

The result already contains subtotals, so you'd double or triple count. Use the grand-total row.

Your mission

A finance-ready table

~25 min

Quick check

Which groupings does GROUP BY ROLLUP (state, city) produce?

Go deeper

  1. 1Produce sales by region and category with subtotals per region and a grand total.
  2. 2Use GROUPING() or COALESCE to label subtotal rows clearly.
  3. 3Compare the output with running several GROUP BY queries joined by UNION ALL.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge