Bird
Raised Fist0
PostgreSQLquery~20 mins

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

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

Practice

(1/5)
1. What does the ROLLUP operator do in a PostgreSQL GROUP BY clause?
easy
A. It filters rows based on a condition before grouping.
B. It creates hierarchical subtotals from left to right for the grouped columns.
C. It generates all possible combinations of grouped columns for totals.
D. It sorts the result set after grouping.

Solution

  1. Step 1: Understand ROLLUP purpose

    ROLLUP creates subtotals that roll up from the first column to the last in the GROUP BY list.
  2. Step 2: Compare with other options

    CUBE creates all combinations, filtering and sorting are unrelated to ROLLUP.
  3. Final Answer:

    It creates hierarchical subtotals from left to right for the grouped columns. -> Option B
  4. Quick Check:

    ROLLUP = hierarchical subtotals [OK]
Hint: ROLLUP = subtotals in order of columns [OK]
Common Mistakes:
  • Confusing ROLLUP with CUBE
  • Thinking ROLLUP filters rows
  • Assuming ROLLUP sorts results
2. Which of the following is the correct syntax to use CUBE in a PostgreSQL query?
easy
A. SELECT col1, col2, SUM(sales) FROM table CUBE BY (col1, col2);
B. SELECT col1, col2, SUM(sales) FROM table GROUP BY CUBE col1, col2;
C. SELECT col1, col2, SUM(sales) FROM table GROUP BY ROLLUP(col1, col2);
D. SELECT col1, col2, SUM(sales) FROM table GROUP BY CUBE(col1, col2);

Solution

  1. Step 1: Recall correct GROUP BY syntax with CUBE

    The correct syntax is GROUP BY CUBE(col1, col2) with parentheses around columns.
  2. Step 2: Identify incorrect options

    SELECT col1, col2, SUM(sales) FROM table CUBE BY (col1, col2); uses invalid 'CUBE BY', C uses ROLLUP, D misses parentheses.
  3. Final Answer:

    SELECT col1, col2, SUM(sales) FROM table GROUP BY CUBE(col1, col2); -> Option D
  4. Quick Check:

    GROUP BY CUBE(columns) syntax [OK]
Hint: Use GROUP BY CUBE(col1, col2) with parentheses [OK]
Common Mistakes:
  • Omitting parentheses in CUBE
  • Using 'CUBE BY' instead of 'GROUP BY CUBE'
  • Confusing ROLLUP and CUBE syntax
3. Given the table sales with columns region, product, and amount, what will be the output rows count of this query?
SELECT region, product, SUM(amount) FROM sales GROUP BY ROLLUP(region, product);
medium
A. Number of distinct (region, product) pairs plus one subtotal per region plus one grand total row.
B. Number of distinct (region, product) pairs only.
C. Number of distinct regions only.
D. Number of distinct products only.

Solution

  1. Step 1: Understand ROLLUP output rows

    ROLLUP(region, product) returns all (region, product) groups, plus subtotals per region, plus a grand total row.
  2. Step 2: Count rows

    Total rows = distinct (region, product) + distinct regions (subtotals) + 1 (grand total).
  3. Final Answer:

    Number of distinct (region, product) pairs plus one subtotal per region plus one grand total row. -> Option A
  4. Quick Check:

    ROLLUP adds subtotals and grand total [OK]
Hint: ROLLUP adds subtotals and grand total rows [OK]
Common Mistakes:
  • Ignoring subtotal and grand total rows
  • Counting only distinct pairs
  • Confusing ROLLUP with CUBE output
4. Identify the error in this query using CUBE:
SELECT region, product, SUM(amount) FROM sales GROUP BY CUBE region, product;
medium
A. GROUP BY cannot use CUBE with more than one column.
B. SUM(amount) cannot be used with CUBE.
C. Missing parentheses around columns in CUBE function.
D. region and product must be in ORDER BY, not GROUP BY.

Solution

  1. Step 1: Check CUBE syntax

    CUBE requires parentheses around the columns: CUBE(region, product).
  2. Step 2: Validate other parts

    SUM(amount) is valid, multiple columns allowed, ORDER BY is unrelated.
  3. Final Answer:

    Missing parentheses around columns in CUBE function. -> Option C
  4. Quick Check:

    CUBE syntax needs parentheses [OK]
Hint: Always use parentheses with CUBE columns [OK]
Common Mistakes:
  • Omitting parentheses in CUBE
  • Thinking SUM is invalid with CUBE
  • Confusing GROUP BY and ORDER BY
5. You want to generate a report showing sales totals by region, product, and year, including all subtotals and grand totals. Which query correctly uses CUBE to achieve this?
hard
A. SELECT region, product, year, SUM(amount) FROM sales GROUP BY CUBE(region, product, year);
B. SELECT region, product, year, SUM(amount) FROM sales GROUP BY region, product, year;
C. SELECT region, product, year, SUM(amount) FROM sales GROUP BY GROUPING SETS(region, product, year);
D. SELECT region, product, year, SUM(amount) FROM sales GROUP BY ROLLUP(region, product, year);

Solution

  1. Step 1: Understand the requirement

    All subtotals and grand totals for region, product, and year means all combinations of these columns.
  2. Step 2: Choose correct grouping method

    CUBE generates all combinations, ROLLUP only hierarchical totals, GROUPING SETS needs explicit sets, simple GROUP BY no subtotals.
  3. Final Answer:

    SELECT region, product, year, SUM(amount) FROM sales GROUP BY CUBE(region, product, year); -> Option A
  4. Quick Check:

    CUBE = all combinations subtotals [OK]
Hint: Use CUBE for all subtotal combinations [OK]
Common Mistakes:
  • Using ROLLUP instead of CUBE for all subtotals
  • Confusing GROUPING SETS with CUBE
  • Forgetting subtotals with simple 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