📖 Scenario: You work in a retail company database. You want to see total sales by category and product, including subtotals for each category and a grand total.
🎯 Goal: Create a SQL query using ROLLUP to get sales subtotals by category and product.
📋 What You'll Learn
Create a table called sales with columns category (VARCHAR), product (VARCHAR), and amount (INT).
Write a SELECT query that sums amount grouped by category and product using GROUP BY ROLLUP(category, product).
Ensure the query returns subtotals per category and a grand total.
💡 Why This Matters
🌍 Real World
Retail and sales databases often need subtotals and grand totals for reports. ROLLUP helps generate these summaries easily.
💼 Career
Knowing how to use ROLLUP is useful for data analysts and database developers to create clear summary reports without complex queries.
Progress0 / 4 steps
1
Create the sales table and insert data
Create a table called sales with columns category as VARCHAR(20), product as VARCHAR(20), and amount as INT. Then insert these exact rows: ('Electronics', 'TV', 100), ('Electronics', 'Radio', 50), ('Clothing', 'Shirt', 40), ('Clothing', 'Pants', 60).
MySQL
Hint
Use CREATE TABLE to define the table and INSERT INTO to add the rows.
2
Set up the SELECT statement with grouping
Write a SELECT statement that selects category, product, and the sum of amount as total_sales from the sales table. Add a GROUP BY clause grouping by category and product. Do not use ROLLUP yet.
MySQL
Hint
Use SUM(amount) AS total_sales and group by both category and product.
3
Add ROLLUP to get subtotals
Modify the GROUP BY clause to use ROLLUP(category, product) to get subtotals for each category and a grand total.
MySQL
Hint
Replace GROUP BY category, product with GROUP BY ROLLUP(category, product).
4
Complete query with ordering for clarity
Add an ORDER BY clause to order results by category ascending and product ascending to make subtotals clear.
MySQL
Hint
Use ORDER BY category ASC, product ASC after the GROUP BY ROLLUP clause.
Practice
(1/5)
1. What does the ROLLUP keyword do in a MySQL GROUP BY query?
easy
A. It adds subtotal and total rows automatically in the grouped result.
B. It filters out NULL values from the result set.
C. It sorts the result set in descending order.
D. It creates a backup of the grouped data.
Solution
Step 1: Understand GROUP BY basics
GROUP BY groups rows by column values to aggregate data.
Step 2: Role of ROLLUP in GROUP BY
ROLLUP adds extra rows showing subtotals and grand totals for grouped data.
Final Answer:
It adds subtotal and total rows automatically in the grouped result. -> Option A
Quick Check:
ROLLUP = subtotals and totals [OK]
Hint: ROLLUP adds summary rows automatically [OK]
Common Mistakes:
Thinking ROLLUP filters data
Confusing ROLLUP with ORDER BY
Assuming ROLLUP creates backups
2. Which of the following is the correct syntax to use ROLLUP in a MySQL query?
easy
A. SELECT category, SUM(amount) FROM sales GROUP BY ROLLUP(category);
B. SELECT category, SUM(amount) FROM sales ROLLUP GROUP BY category;
C. SELECT category, SUM(amount) FROM sales GROUP BY category WITH ROLLUP;
D. SELECT category, SUM(amount) FROM sales GROUP BY category ROLLUP;
Solution
Step 1: Recall correct ROLLUP syntax
In MySQL, ROLLUP is used as GROUP BY ... WITH ROLLUP.
Step 2: Analyze each option
SELECT category, SUM(amount) FROM sales GROUP BY category WITH ROLLUP; uses GROUP BY category WITH ROLLUP, which is correct syntax. Others misuse ROLLUP placement or syntax.
Final Answer:
SELECT category, SUM(amount) FROM sales GROUP BY category WITH ROLLUP; -> Option C
Quick Check:
ROLLUP goes after GROUP BY with WITH keyword [OK]
Hint: Use 'GROUP BY ... WITH ROLLUP' syntax [OK]
Common Mistakes:
Placing ROLLUP before GROUP BY
Using ROLLUP as a function
Omitting WITH keyword
3. Given the table sales with columns region, product, and amount, what will be the output of this query?
SELECT region, product, SUM(amount) AS total_sales
FROM sales
GROUP BY region, product WITH ROLLUP;
medium
A. Rows grouped by region and product, plus subtotals by region and a grand total row with NULLs.
B. Only rows grouped by region and product without any subtotal or total rows.
C. Rows grouped by product only, ignoring region.
D. Syntax error due to incorrect use of ROLLUP.
Solution
Step 1: Understand GROUP BY with multiple columns and ROLLUP
ROLLUP creates subtotals for each grouping level: first by region and product, then by region only, then grand total.
Step 2: Interpret NULL values in output
Subtotal rows have NULL in the column(s) rolled up. The grand total row has NULL in both region and product.
Final Answer:
Rows grouped by region and product, plus subtotals by region and a grand total row with NULLs. -> Option A
Quick Check:
ROLLUP adds subtotal and total rows with NULL markers [OK]
Hint: ROLLUP adds subtotal rows with NULLs in rolled-up columns [OK]
Common Mistakes:
Expecting no subtotal rows
Confusing grouping levels
Thinking NULL means missing data
4. You wrote this query but it gives an error:
SELECT region, product, SUM(amount) FROM sales GROUP BY region, product ROLLUP;
What is the error and how to fix it?
medium
A. ROLLUP cannot be used with multiple columns in GROUP BY.
B. GROUP BY clause must come after WHERE clause.
C. SUM() function is not allowed with ROLLUP.
D. ROLLUP must be used with the keyword WITH after GROUP BY.
Solution
Step 1: Identify syntax error in ROLLUP usage
ROLLUP must be used as GROUP BY ... WITH ROLLUP, not just appended after GROUP BY columns.
Step 2: Correct the query syntax
Change query to GROUP BY region, product WITH ROLLUP to fix the error.
Final Answer:
ROLLUP must be used with the keyword WITH after GROUP BY. -> Option D
Quick Check:
ROLLUP requires WITH keyword [OK]
Hint: Use 'WITH ROLLUP' after GROUP BY columns [OK]
Common Mistakes:
Omitting WITH keyword
Misplacing ROLLUP before GROUP BY
Thinking ROLLUP disallows multiple columns
5. You want to get sales totals by region and product, plus subtotals by region and a grand total. Which query correctly uses ROLLUP and filters out only the grand total row from the result?
hard
A. SELECT region, product, SUM(amount) FROM sales GROUP BY region, product WITH ROLLUP HAVING region IS NOT NULL;
B. SELECT region, product, SUM(amount) FROM sales GROUP BY region, product WITH ROLLUP HAVING NOT (region IS NULL AND product IS NULL);
C. SELECT region, product, SUM(amount) FROM sales GROUP BY region, product WITH ROLLUP HAVING product IS NOT NULL;
D. SELECT region, product, SUM(amount) FROM sales GROUP BY region, product WITH ROLLUP HAVING region IS NULL AND product IS NULL;
Solution
Step 1: Understand ROLLUP output rows
ROLLUP adds subtotal rows with NULLs in rolled-up columns; grand total row has NULL in all grouped columns.
Step 2: Filter out only the grand total row
The grand total row has both region and product as NULL. To exclude it, use HAVING NOT (region IS NULL AND product IS NULL).
Final Answer:
SELECT region, product, SUM(amount) FROM sales GROUP BY region, product WITH ROLLUP HAVING NOT (region IS NULL AND product IS NULL); -> Option B
Quick Check:
Exclude grand total by filtering NULLs in all grouping columns [OK]
Hint: Exclude grand total by HAVING NOT all grouping columns NULL [OK]
Common Mistakes:
Filtering only one column NULL
Filtering to keep only grand total instead of excluding
Using WHERE instead of HAVING with GROUP BY
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.