Bird
Raised Fist0
MySQLquery~20 mins

ROLLUP for subtotals in MySQL - Practice Problems & Coding Challenges

Choose your learning style10 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.

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

  1. Step 1: Understand GROUP BY basics

    GROUP BY groups rows by column values to aggregate data.
  2. Step 2: Role of ROLLUP in GROUP BY

    ROLLUP adds extra rows showing subtotals and grand totals for grouped data.
  3. Final Answer:

    It adds subtotal and total rows automatically in the grouped result. -> Option A
  4. 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

  1. Step 1: Recall correct ROLLUP syntax

    In MySQL, ROLLUP is used as GROUP BY ... WITH ROLLUP.
  2. 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.
  3. Final Answer:

    SELECT category, SUM(amount) FROM sales GROUP BY category WITH ROLLUP; -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Rows grouped by region and product, plus subtotals by region and a grand total row with NULLs. -> Option A
  4. 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

  1. Step 1: Identify syntax error in ROLLUP usage

    ROLLUP must be used as GROUP BY ... WITH ROLLUP, not just appended after GROUP BY columns.
  2. Step 2: Correct the query syntax

    Change query to GROUP BY region, product WITH ROLLUP to fix the error.
  3. Final Answer:

    ROLLUP must be used with the keyword WITH after GROUP BY. -> Option D
  4. 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

  1. 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.
  2. 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).
  3. 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
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard