Challenge - 5 Problems
ROLLUP Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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);
Attempts:
2 left
💡 Hint
ROLLUP adds subtotal rows for each grouping level and a grand total row.
✗ Incorrect
ROLLUP creates subtotals for each grouping column from right to left and a grand total row with NULLs in all grouping columns.
🧠 Conceptual
intermediate1:30remaining
How does ROLLUP generate subtotal rows?
Which statement best describes how the
ROLLUP operator works in a GROUP BY clause?Attempts:
2 left
💡 Hint
Think about how grouping columns are reduced step by step.
✗ Incorrect
ROLLUP creates subtotals by grouping on all columns, then removing the last column, then the second last, and so on, producing subtotals and a grand total.
📝 Syntax
advanced2: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.Attempts:
2 left
💡 Hint
ROLLUP is used as a function inside GROUP BY in MySQL.
✗ Incorrect
In MySQL, ROLLUP is used as GROUP BY ROLLUP(column1, column2). The syntax 'WITH ROLLUP' is deprecated but still works in some versions; however, the question asks for the correct modern syntax.
❓ optimization
advanced2: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?Attempts:
2 left
💡 Hint
Indexes help speed up grouping and aggregation.
✗ Incorrect
Indexes on grouping columns help the database quickly group and aggregate data, improving performance of ROLLUP queries.
🔧 Debug
expert3:00remaining
Why does this ROLLUP query produce unexpected NULLs?
Consider this query:
Why do some rows have NULL in
SELECT department, role, SUM(salary) FROM employees GROUP BY ROLLUP(department, role);Why do some rows have NULL in
department or role?Attempts:
2 left
💡 Hint
Think about what ROLLUP adds to the result set.
✗ Incorrect
ROLLUP adds subtotal rows by grouping on fewer columns, which results in NULLs in the columns that are not grouped at that level.