0
0
MySQLquery~20 mins

ROLLUP for subtotals in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ROLLUP Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this ROLLUP query?
Given a sales table with columns region, category, and amount, what will be the output of this query?

SELECT region, category, SUM(amount) AS total_sales FROM sales GROUP BY region, category WITH ROLLUP;
MySQL
CREATE TABLE sales (region VARCHAR(20), category VARCHAR(20), amount INT);
INSERT INTO sales VALUES
('North', 'Books', 100),
('North', 'Electronics', 200),
('South', 'Books', 150),
('South', 'Electronics', 250);
ASyntax error because ROLLUP cannot be used with multiple columns
BRows showing sums only for each region-category pair without any subtotal or grand total
CRows showing sums for each category only, ignoring region
DRows showing sums for each region-category pair, each region total, and a grand total row with NULLs in region and category
Attempts:
2 left
💡 Hint
ROLLUP adds subtotal rows for each grouping level and a grand total row.
🧠 Conceptual
intermediate
1:30remaining
How does ROLLUP generate subtotal rows?
Which statement best describes how the ROLLUP operator works in a GROUP BY clause?
AIt duplicates all rows without grouping
BIt adds subtotal rows by progressively removing grouping columns from right to left
CIt only adds a grand total row without subtotals
DIt sorts the result set without aggregation
Attempts:
2 left
💡 Hint
Think about how grouping columns are reduced step by step.
📝 Syntax
advanced
2:00remaining
Which query correctly uses ROLLUP for subtotals?
Choose the valid MySQL query that uses ROLLUP to get subtotals by department and role from an employees table.
ASELECT department, role, COUNT(*) FROM employees GROUP BY ROLLUP(department, role);
BSELECT department, role, COUNT(*) FROM employees ROLLUP BY department, role;
CSELECT department, role, COUNT(*) FROM employees GROUP BY department, role WITH ROLLUP;
DSELECT department, role, COUNT(*) FROM employees GROUP BY department, role ROLLUP;
Attempts:
2 left
💡 Hint
ROLLUP is used as a function inside GROUP BY in MySQL.
optimization
advanced
2:30remaining
How to optimize a query with ROLLUP on large data?
You have a large sales table and use GROUP BY ROLLUP(region, product). Which approach best improves performance?
AUse SELECT * instead of specifying columns to reduce parsing time
BRemove ROLLUP and calculate subtotals manually in application code
CCreate indexes on the grouping columns <code>region</code> and <code>product</code>
DAdd ORDER BY clauses on non-grouped columns
Attempts:
2 left
💡 Hint
Indexes help speed up grouping and aggregation.
🔧 Debug
expert
3:00remaining
Why does this ROLLUP query produce unexpected NULLs?
Consider this query:

SELECT department, role, SUM(salary) FROM employees GROUP BY ROLLUP(department, role);

Why do some rows have NULL in department or role?
ANULLs represent subtotal or grand total rows created by ROLLUP
BNULLs indicate missing data in the original table
CNULLs appear because the GROUP BY columns are not indexed
DThe query has a syntax error causing NULLs to appear
Attempts:
2 left
💡 Hint
Think about what ROLLUP adds to the result set.