Cheat SheetsSQLAggregations

Aggregations — Cheat Sheet

SQL · 1 topics. Download the PDF or the Instagram carousel and share it.

Cheat Sheet · AiCanCode.org
Aggregations
SQL1 topicsQuick revision reference
1

GROUP BY ROLLUP & CUBE

ROLLUP and CUBE are extensions to GROUP BY that generate subtotals and grand totals for hierarchical or multi-dimensional data.

SQL — GROUP BY ROLLUP example
-- Sales subtotals by region, then product
SELECT region, product, SUM(sales) AS total_sales
FROM sales
GROUP BY ROLLUP (region, product);

-- Output:
-- region   | product   | total_sales
-- -------- | --------- | -----------
-- North    | Widget A  | 1000
-- North    | Widget B  | 2000
-- North    | NULL      | 3000  -- subtotal for North
-- NULL     | NULL      | 6000  -- grand total
Learn this free with Aria, your AI tutor → AiCanCode.org/learn/sql