0
0
PostgreSQLquery~5 mins

ROLLUP and CUBE for hierarchical totals in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the ROLLUP operator do in SQL?
ROLLUP creates hierarchical totals by grouping data at multiple levels, starting from detailed rows up to a grand total.
Click to reveal answer
intermediate
How does CUBE differ from ROLLUP in SQL?
CUBE generates all possible combinations of grouping columns, giving totals for every grouping level, not just hierarchical ones.
Click to reveal answer
beginner
Write a simple SQL query using ROLLUP to get sales totals by region and product category.
SELECT region, category, SUM(sales) FROM sales_data GROUP BY ROLLUP(region, category);
Click to reveal answer
intermediate
What is the output difference between GROUP BY ROLLUP(a, b) and GROUP BY CUBE(a, b)?
ROLLUP(a, b) returns totals for (a, b), (a), and grand total. CUBE(a, b) returns totals for (a, b), (a), (b), and grand total.
Click to reveal answer
beginner
Why are ROLLUP and CUBE useful in reports?
They help create summary rows automatically, showing subtotals and grand totals without extra queries.
Click to reveal answer
What does GROUP BY ROLLUP(a, b) produce?
ATotals for (a, b), (a), and grand total
BTotals for (a, b), (b), and grand total
CTotals for (a), (b), and grand total
DTotals for (a, b) only
Which operator generates all combinations of grouping columns?
AROLLUP
BCUBE
CGROUPING SETS
DHAVING
In PostgreSQL, which clause can be used to get subtotals and grand totals in one query?
AORDER BY
BLIMIT
CWHERE
DGROUP BY ROLLUP
If you want totals for each column individually and combined, which should you use?
AROLLUP
BDISTINCT
CCUBE
DUNION
What keyword helps identify subtotal rows in ROLLUP or CUBE results?
AGROUPING
BHAVING
CORDER BY
DWHERE
Explain how ROLLUP works to create hierarchical totals in a sales report.
Think about how totals build up from detailed to summary levels.
You got /4 concepts.
    Describe the difference between CUBE and ROLLUP and when you might use each.
    Consider the number of subtotal rows and grouping combinations.
    You got /4 concepts.