ROLLUP and CUBE for hierarchical totals in PostgreSQL - Time & Space Complexity
When using ROLLUP and CUBE in SQL, we want to know how the time to get results changes as the data grows.
We ask: How does adding more data or grouping columns affect the work the database does?
Analyze the time complexity of this SQL query using ROLLUP.
SELECT region, product, SUM(sales) AS total_sales
FROM sales_data
GROUP BY ROLLUP(region, product);
This query calculates sales totals for each region and product, plus subtotals and a grand total.
Look for repeated work done by the database.
- Primary operation: Scanning all rows in the sales_data table to sum sales.
- How many times: Once for the full data, but grouping creates multiple subtotal calculations.
As the number of rows (n) grows, the database must process more data to sum.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 sums plus grouping overhead |
| 100 | About 100 sums plus grouping overhead |
| 1000 | About 1000 sums plus grouping overhead |
Pattern observation: The work grows roughly in direct proportion to the number of rows.
Time Complexity: O(n log n)
This means the time to run the query grows roughly in proportion to n log n as the data size increases, due to grouping overhead.
[X] Wrong: "ROLLUP or CUBE makes the query take exponentially longer because it does many groupings."
[OK] Correct: The database efficiently computes subtotals during one pass, so the main cost is still scanning all rows once plus grouping overhead.
Understanding how grouping with ROLLUP or CUBE affects performance helps you explain query costs clearly and shows you know how databases handle summaries.
"What if we add more columns to the ROLLUP list? How would that change the time complexity?"
Practice
ROLLUP operator do in a PostgreSQL GROUP BY clause?Solution
Step 1: Understand ROLLUP purpose
ROLLUP creates subtotals that roll up from the first column to the last in the GROUP BY list.Step 2: Compare with other options
CUBE creates all combinations, filtering and sorting are unrelated to ROLLUP.Final Answer:
It creates hierarchical subtotals from left to right for the grouped columns. -> Option BQuick Check:
ROLLUP = hierarchical subtotals [OK]
- Confusing ROLLUP with CUBE
- Thinking ROLLUP filters rows
- Assuming ROLLUP sorts results
CUBE in a PostgreSQL query?Solution
Step 1: Recall correct GROUP BY syntax with CUBE
The correct syntax is GROUP BY CUBE(col1, col2) with parentheses around columns.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.Final Answer:
SELECT col1, col2, SUM(sales) FROM table GROUP BY CUBE(col1, col2); -> Option DQuick Check:
GROUP BY CUBE(columns) syntax [OK]
- Omitting parentheses in CUBE
- Using 'CUBE BY' instead of 'GROUP BY CUBE'
- Confusing ROLLUP and CUBE syntax
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);
Solution
Step 1: Understand ROLLUP output rows
ROLLUP(region, product) returns all (region, product) groups, plus subtotals per region, plus a grand total row.Step 2: Count rows
Total rows = distinct (region, product) + distinct regions (subtotals) + 1 (grand total).Final Answer:
Number of distinct (region, product) pairs plus one subtotal per region plus one grand total row. -> Option AQuick Check:
ROLLUP adds subtotals and grand total [OK]
- Ignoring subtotal and grand total rows
- Counting only distinct pairs
- Confusing ROLLUP with CUBE output
CUBE:SELECT region, product, SUM(amount) FROM sales GROUP BY CUBE region, product;
Solution
Step 1: Check CUBE syntax
CUBE requires parentheses around the columns: CUBE(region, product).Step 2: Validate other parts
SUM(amount) is valid, multiple columns allowed, ORDER BY is unrelated.Final Answer:
Missing parentheses around columns in CUBE function. -> Option CQuick Check:
CUBE syntax needs parentheses [OK]
- Omitting parentheses in CUBE
- Thinking SUM is invalid with CUBE
- Confusing GROUP BY and ORDER BY
region, product, and year, including all subtotals and grand totals. Which query correctly uses CUBE to achieve this?Solution
Step 1: Understand the requirement
All subtotals and grand totals for region, product, and year means all combinations of these columns.Step 2: Choose correct grouping method
CUBE generates all combinations, ROLLUP only hierarchical totals, GROUPING SETS needs explicit sets, simple GROUP BY no subtotals.Final Answer:
SELECT region, product, year, SUM(amount) FROM sales GROUP BY CUBE(region, product, year); -> Option AQuick Check:
CUBE = all combinations subtotals [OK]
- Using ROLLUP instead of CUBE for all subtotals
- Confusing GROUPING SETS with CUBE
- Forgetting subtotals with simple GROUP BY
Ready to prove it?
Pass the test and this pattern moves unknown
