0
0
MySQLquery~10 mins

ROLLUP for subtotals in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ROLLUP for subtotals
Start with GROUP BY columns
Aggregate rows by groups
Add subtotal rows for each grouping level
Add grand total row
Return result with subtotals and total
ROLLUP groups rows and adds extra rows for subtotals and a grand total automatically.
Execution Sample
MySQL
SELECT category, product, SUM(sales) AS total_sales
FROM sales_data
GROUP BY category, product WITH ROLLUP;
This query sums sales by category and product, then adds subtotal rows for each category and a grand total.
Execution Table
StepcategoryproductSUM(sales)Explanation
1ElectronicsPhone1000Sum sales for Electronics Phone
2ElectronicsLaptop1500Sum sales for Electronics Laptop
3Electronics-2500Subtotal for Electronics category (Phone + Laptop)
4FurnitureChair700Sum sales for Furniture Chair
5FurnitureTable300Sum sales for Furniture Table
6Furniture-1000Subtotal for Furniture category (Chair + Table)
7--3500Grand total for all categories and products
💡 All groups processed, subtotals and grand total rows added by ROLLUP
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
categorynullElectronicsElectronicsElectronicsFurnitureFurnitureFurniturenull
productnullPhoneLaptopnullChairTablenullnull
SUM(sales)010001500250070030010003500
Key Moments - 3 Insights
Why does the product column show null in some rows?
Rows with null in product are subtotal rows for the category, as shown in execution_table rows 3 and 6.
What does the row with both category and product as null represent?
That row is the grand total for all categories and products, shown in execution_table row 7.
How does ROLLUP decide which subtotal rows to add?
ROLLUP adds subtotal rows by progressively removing columns from right to left in the GROUP BY, as seen in the subtotal rows with null product.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the SUM(sales) value at step 3?
A2500
B1500
C1000
D3500
💡 Hint
Check the row where category is Electronics and product is null in execution_table.
At which step does the grand total appear in the execution_table?
AStep 6
BStep 7
CStep 3
DStep 1
💡 Hint
Look for the row where both category and product are null.
If we remove product from GROUP BY and use ROLLUP on category only, how would the subtotal rows change?
AThere would be no subtotal rows
BSubtotal rows would be for product only
CSubtotal rows would be for category only, no product breakdown
DGrand total row would be removed
💡 Hint
ROLLUP adds subtotals by removing columns from right to left in GROUP BY.
Concept Snapshot
ROLLUP adds subtotal and total rows automatically.
Use WITH ROLLUP after GROUP BY.
It groups by all columns, then adds subtotals by removing columns from right.
Nulls in grouped columns mark subtotal or total rows.
Useful for quick summary reports.
Full Transcript
ROLLUP is a SQL feature that helps you get subtotals and grand totals easily. When you group data by columns and add WITH ROLLUP, the database first groups rows normally. Then it adds extra rows that show subtotals for each group level by replacing the rightmost grouped columns with null. Finally, it adds a grand total row with all grouped columns as null. For example, grouping sales by category and product with ROLLUP will show sales per product, subtotals per category, and a grand total. Null values in grouped columns indicate subtotal or total rows. This makes reports easier to read and saves writing multiple queries.