ROLLUP & CUBE: Mistakes
Module: Aggregate Functions & Grouping
SELECT region, country, SUM(sales) FROM orders GROUP BY ROLLUP(region, country) ORDER BY region, country;
SELECT region, country, SUM(sales) FROM orders GROUP BY ROLLUP(region, country) ORDER BY region NULLS LAST, country NULLS LAST;
Without NULLS LAST, subtotal rows (with NULL) appear before detail rows. Use NULLS LAST for logical ordering.
Always use NULLS LAST in ORDER BY with ROLLUP/CUBE
Medium
NULL subtotal rows appear first without NULLS LAST
SELECT region, SUM(sales) FROM orders GROUP BY CUBE(region, country);
SELECT region, country, SUM(sales) FROM orders GROUP BY CUBE(region, country);
All columns in ROLLUP/CUBE must appear in SELECT to see subtotal structure.
Include all ROLLUP/CUBE columns in SELECT
High
Missing country in SELECT but included in CUBE