GROUPING SETS / ROLLUP / CUBE
Extensions to GROUP BY that compute aggregates across multiple grouping combinations in a single query.
GROUPING SETS ROLLUP CUBE GROUP BY ROLLUP multi-dimensional aggregation GROUPING()
aggregation GROUP BY ROLLUP CUBE analytics reporting
| Database | Status | Syntax Family | Min Version |
|---|---|---|---|
| PostgreSQL | full | sql-standard | 9.5 |
| MySQL | partial | rollup-only | - |
| MariaDB | partial | rollup-only | - |
| Oracle | full | sql-standard | - |
| SQL Server | full | sql-standard | 2008 |
| SQLite | none | - | - |
| Redshift | full | sql-standard | - |
| DB2 | full | sql-standard | - |
| Snowflake | full | sql-standard | - |
| BigQuery | full | sql-standard | - |
Compare Databases
Database Details
PostgreSQL
full
SELECT
region,
product,
SUM(sales) AS total_sales
FROM sales_data
GROUP BY GROUPING SETS (
(region, product),
(region),
(product),
()
)
ORDER BY region NULLS LAST, product NULLS LAST;- ROLLUP, CUBE, and GROUPING SETS were all added in 9.5.
- Multiple grouping specifications can be combined in one GROUP BY clause.
MySQL
partial
SELECT region, product, SUM(sales) AS total_sales
FROM sales_data
GROUP BY region, product WITH ROLLUP;- ROLLUP only; no CUBE or GROUPING SETS.
- GROUPING() was added in 8.0.
Docs: GROUP BY Modifiers
MariaDB
partial
SELECT region, product, SUM(sales) AS total_sales
FROM sales_data
GROUP BY region, product WITH ROLLUP;- ROLLUP only, with MySQL's WITH ROLLUP trailing-modifier spelling. No CUBE or GROUPING SETS.
Oracle
full
SELECT
region,
product,
SUM(sales) AS total_sales
FROM sales_data
GROUP BY GROUPING SETS (
(region, product),
(region),
(product),
()
)
ORDER BY region NULLS LAST, product NULLS LAST;- Oracle shipped ROLLUP and CUBE first among major vendors (8i).
- Supports composite columns and concatenated grouping sets.
SQL Server
full
SELECT
region,
product,
SUM(sales) AS total_sales
FROM sales_data
GROUP BY GROUPING SETS (
(region, product),
(region),
(product),
()
)
ORDER BY region, product;- All three added in 2008.
- GROUPING_ID() returns a bitmap of which columns a given row aggregates over — useful when multiple groupings are combined.
Docs: GROUP BY (Transact-SQL)
Redshift
full
SELECT
region,
product,
SUM(sales) AS total_sales
FROM sales_data
GROUP BY GROUPING SETS (
(region, product),
(region),
(product),
()
)
ORDER BY region NULLS LAST, product NULLS LAST;Docs: GROUP BY (Redshift)
DB2
full
SELECT
region,
product,
SUM(sales) AS total_sales
FROM sales_data
GROUP BY GROUPING SETS (
(region, product),
(region),
(product),
()
)
ORDER BY region, product;
Snowflake
full
SELECT
region,
product,
SUM(sales) AS total_sales
FROM sales_data
GROUP BY GROUPING SETS (
(region, product),
(region),
(product),
()
)
ORDER BY region NULLS LAST, product NULLS LAST;
BigQuery
full
SELECT
region,
product,
SUM(sales) AS total_sales
FROM sales_data
GROUP BY GROUPING SETS (
(region, product),
(region),
(product),
()
)
ORDER BY region NULLS LAST, product NULLS LAST;- ROLLUP was there from the start of GoogleSQL; CUBE and GROUPING SETS arrived in 2022.