ROLLUP and CUBE help you quickly get totals and subtotals for groups of data. They make it easy to see summaries at different levels.
ROLLUP and CUBE for hierarchical totals in PostgreSQL
SELECT columns, AGGREGATE_FUNCTION(column) FROM table GROUP BY ROLLUP(column1, column2, ...); -- or SELECT columns, AGGREGATE_FUNCTION(column) FROM table GROUP BY CUBE(column1, column2, ...);
ROLLUP creates subtotals moving from left to right in the list of columns, ending with a grand total.
CUBE creates subtotals for all combinations of the listed columns, including the grand total.
SELECT region, country, SUM(sales) AS total_sales FROM sales_data GROUP BY ROLLUP(region, country);
SELECT department, project, SUM(cost) AS total_cost FROM expenses GROUP BY CUBE(department, project);
This example creates a sales table, inserts sample data, and uses ROLLUP to get totals by region, country, and grand total.
CREATE TABLE sales ( region TEXT, country TEXT, sales INT ); INSERT INTO sales VALUES ('North America', 'USA', 100), ('North America', 'Canada', 150), ('Europe', 'France', 200), ('Europe', 'Germany', 250); SELECT region, country, SUM(sales) AS total_sales FROM sales GROUP BY ROLLUP(region, country) ORDER BY region NULLS LAST, country NULLS LAST;
NULL values in the result represent subtotal or grand total rows.
Use ORDER BY with NULLS LAST to keep totals at the bottom for better readability.
ROLLUP is good for hierarchical totals; CUBE is better when you want all combinations.
ROLLUP and CUBE help create subtotals and grand totals in one query.
ROLLUP gives hierarchical totals from left to right.
CUBE gives all combinations of totals for the listed columns.
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
