What if you could get all your sales summaries in one simple step, without any manual adding?
Why ROLLUP and CUBE for hierarchical totals in PostgreSQL? - Purpose & Use Cases
Imagine you run a store and want to see sales totals by category, then by category and subcategory, and finally the grand total. Doing this by hand means adding up many numbers from different lists and spreadsheets.
Manually summing totals for each level is slow and easy to mess up. You might forget a category or add numbers twice. It's hard to keep track and update when new sales come in.
ROLLUP and CUBE let the database do all the adding for you. They automatically create totals for each level of detail you want, so you get all the sums in one simple query.
SELECT category, subcategory, SUM(sales) FROM sales_data GROUP BY category, subcategory; -- Then manually add totals for categories and grand total
SELECT category, subcategory, SUM(sales) FROM sales_data GROUP BY ROLLUP(category, subcategory);
You can quickly get detailed and summary totals together, making it easy to understand your data at every level.
A store manager sees sales by product type, then by product type and brand, plus overall sales, all in one report without extra work.
Manual totals are slow and error-prone.
ROLLUP and CUBE automate hierarchical totals in one query.
This saves time and gives clear, complete summaries.
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
