0
0
PostgreSQLquery~20 mins

ROLLUP and CUBE for hierarchical totals in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of ROLLUP and CUBE
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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);
A[{"region":"East","product":null,"total_sales":30},{"region":"West","product":null,"total_sales":40},{"region":null,"product":null,"total_sales":70}]
B[{"region":"East","product":"Pen","total_sales":10},{"region":"East","product":"Pencil","total_sales":20},{"region":"West","product":"Pen","total_sales":15},{"region":"West","product":"Pencil","total_sales":25},{"region":null,"product":null,"total_sales":70}]
C[{"region":"East","product":"Pen","total_sales":10},{"region":"East","product":"Pencil","total_sales":20},{"region":"East","product":null,"total_sales":30},{"region":"West","product":"Pen","total_sales":15},{"region":"West","product":"Pencil","total_sales":25},{"region":"West","product":null,"total_sales":40},{"region":null,"product":null,"total_sales":70}]
D[{"region":"East","product":"Pen","total_sales":10},{"region":"East","product":"Pencil","total_sales":20},{"region":"West","product":"Pen","total_sales":15},{"region":"West","product":"Pencil","total_sales":25}]
Attempts:
2 left
💡 Hint
ROLLUP adds subtotals for each grouping level, including grand total.
query_result
intermediate
2: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);
A[{"region":"East","product":"Pen","total_sales":10},{"region":"East","product":"Pencil","total_sales":20},{"region":"East","product":null,"total_sales":30},{"region":"West","product":"Pen","total_sales":15},{"region":"West","product":"Pencil","total_sales":25},{"region":"West","product":null,"total_sales":40},{"region":null,"product":"Pen","total_sales":25},{"region":null,"product":"Pencil","total_sales":45},{"region":null,"product":null,"total_sales":70}]
B[{"region":"East","product":"Pen","total_sales":10},{"region":"East","product":"Pencil","total_sales":20},{"region":"West","product":"Pen","total_sales":15},{"region":"West","product":"Pencil","total_sales":25},{"region":null,"product":null,"total_sales":70}]
C[{"region":"East","product":null,"total_sales":30},{"region":"West","product":null,"total_sales":40},{"region":null,"product":null,"total_sales":70}]
D[{"region":"East","product":"Pen","total_sales":10},{"region":"West","product":"Pen","total_sales":15},{"region":null,"product":"Pen","total_sales":25}]
Attempts:
2 left
💡 Hint
CUBE generates all combinations of grouping columns including subtotals for each and grand total.
📝 Syntax
advanced
2:00remaining
Identify syntax error in ROLLUP usage
Which option contains a syntax error when using ROLLUP in PostgreSQL?
ASELECT region, product, SUM(amount) FROM sales GROUP BY ROLLUP(region product);
BSELECT region, product, SUM(amount) FROM sales GROUP BY ROLLUP(region, product);
CSELECT region, product, SUM(amount) FROM sales GROUP BY ROLLUP(region, product) ORDER BY region;
DSELECT region, product, SUM(amount) FROM sales GROUP BY ROLLUP(region, product) HAVING SUM(amount) > 10;
Attempts:
2 left
💡 Hint
Check the syntax of the ROLLUP function arguments.
optimization
advanced
2: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?
AUse GROUP BY CUBE(region, product, year) directly on the large table without indexes.
BCreate indexes on region, product, and year columns before running GROUP BY CUBE(region, product, year).
CRun separate GROUP BY queries for each combination of columns and UNION ALL the results.
DUse GROUP BY ROLLUP(region, product, year) instead of CUBE to reduce combinations.
Attempts:
2 left
💡 Hint
Indexes can speed up grouping operations on large tables.
🧠 Conceptual
expert
2:00remaining
Difference between ROLLUP and CUBE
Which statement correctly describes the difference between ROLLUP and CUBE in SQL grouping?
AROLLUP generates all possible combinations of grouping columns, while CUBE generates hierarchical subtotals.
BROLLUP and CUBE produce the same output but differ in performance.
CROLLUP only works with two columns, while CUBE works with any number of columns.
DROLLUP generates hierarchical subtotals from left to right, while CUBE generates all possible combinations of grouping columns.
Attempts:
2 left
💡 Hint
Think about how many grouping sets each generates.