Challenge - 5 Problems
Master of ROLLUP and CUBE
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of ROLLUP aggregation
Given a sales table with columns
region, product, and amount, what is the output of this query?SELECT region, product, SUM(amount) AS total_sales FROM sales GROUP BY ROLLUP(region, product) ORDER BY region NULLS LAST, product NULLS LAST;
PostgreSQL
CREATE TABLE sales (region TEXT, product TEXT, amount INT); INSERT INTO sales VALUES ('East', 'Pen', 10), ('East', 'Pencil', 20), ('West', 'Pen', 15), ('West', 'Pencil', 25);
Attempts:
2 left
💡 Hint
ROLLUP adds subtotals for each grouping level, including grand total.
✗ Incorrect
ROLLUP(region, product) groups by region and product, then by region only, then grand total (nulls). So it returns rows for each product per region, subtotal per region, and grand total.
❓ query_result
intermediate2:00remaining
Output of CUBE aggregation
Using the same
sales table, what is the output of this query?SELECT region, product, SUM(amount) AS total_sales FROM sales GROUP BY CUBE(region, product) ORDER BY region NULLS LAST, product NULLS LAST;
PostgreSQL
CREATE TABLE sales (region TEXT, product TEXT, amount INT); INSERT INTO sales VALUES ('East', 'Pen', 10), ('East', 'Pencil', 20), ('West', 'Pen', 15), ('West', 'Pencil', 25);
Attempts:
2 left
💡 Hint
CUBE generates all combinations of grouping columns including subtotals for each and grand total.
✗ Incorrect
CUBE(region, product) returns all combinations: by region and product, by region only, by product only, and grand total.
📝 Syntax
advanced2:00remaining
Identify syntax error in ROLLUP usage
Which option contains a syntax error when using ROLLUP in PostgreSQL?
Attempts:
2 left
💡 Hint
Check the syntax of the ROLLUP function arguments.
✗ Incorrect
ROLLUP requires columns separated by commas inside parentheses. Missing comma causes syntax error.
❓ optimization
advanced2:00remaining
Optimizing query with CUBE on large dataset
You have a large sales table with columns
region, product, year, and amount. You want to get all subtotals and grand totals for these three columns using CUBE. Which approach is best to optimize performance?Attempts:
2 left
💡 Hint
Indexes can speed up grouping operations on large tables.
✗ Incorrect
Creating indexes on grouping columns helps the database quickly group data, improving performance of GROUP BY CUBE.
🧠 Conceptual
expert2:00remaining
Difference between ROLLUP and CUBE
Which statement correctly describes the difference between ROLLUP and CUBE in SQL grouping?
Attempts:
2 left
💡 Hint
Think about how many grouping sets each generates.
✗ Incorrect
ROLLUP creates subtotals in a hierarchy from left to right columns. CUBE creates all combinations of grouping columns.