An explicit list of grouping combinations to compute in a single pass, giving precise control over which subtotals are produced. More flexible than ROLLUP or CUBE.
An explicit GROUP BY variant that specifies exactly which grouping combinations to compute. More flexible than ROLLUP or CUBE, and clearer in intent. SELECT region, city, SUM(sales) GROUP BY GROUPING SETS((region), (region, city), ()) computes totals at multiple levels.
| Database System | Support Status | Since Version | Notes |
|---|---|---|---|
| MySQL | ✗ Not Supported | — | GROUPING SETS is not supported. MySQL only supports ROLLUP via GROUP BY ... WITH ROLLUP or GROUP BY ROLLUP(...). For specific grouping combinations, use UNION ALL. |
| PostgreSQL | ✓ Supported | 9.5 | Standard GROUPING SETS syntax. Can be combined with ROLLUP and CUBE in the same GROUP BY clause. GROUPING(col) identifies which rows are grouping-generated. An empty set () produces the grand total. Equivalent to the corresponding UNION ALL but computed in a single scan. |
| SQL Server | ✓ Supported | 2008 | Standard GROUPING SETS syntax since SQL Server 2008. GROUPING() and GROUPING_ID() supported. Useful for reporting queries that need specific subtotal combinations without the full output of CUBE. |
| Oracle | ✓ Supported | 8i | Standard GROUPING SETS syntax since Oracle 8i. Can be combined with ROLLUP and CUBE. GROUPING() and GROUPING_ID() supported. |
| SQLite | ✗ Not Supported | — | GROUPING SETS is not supported. Use UNION ALL to combine individual GROUP BY queries for each desired grouping. |
GROUPING SETS is the most explicit of the three grouping extensions (vs ROLLUP and CUBE): you list exactly the combinations you want, nothing more. ROLLUP(a, b) is syntactic sugar for GROUPING SETS((a,b),(a),()), and CUBE(a,b) is syntactic sugar for GROUPING SETS((a,b),(a),(b),()). MySQL is the only major RDBMS with no support for GROUPING SETS; SQLite supports none of the three. When genuinely NULL data values exist in grouping columns, always use GROUPING() to distinguish subtotal NULLs from real NULLs — COALESCE alone is unreliable in that case.
GROUPING SETS is not supported. MySQL only supports ROLLUP via GROUP BY ... WITH ROLLUP or GROUP BY ROLLUP(...). For specific grouping combinations, use UNION ALL.
Standard GROUPING SETS syntax. Can be combined with ROLLUP and CUBE in the same GROUP BY clause. GROUPING(col) identifies which rows are grouping-generated. An empty set () produces the grand total. Equivalent to the corresponding UNION ALL but computed in a single scan.
Standard GROUPING SETS syntax since SQL Server 2008. GROUPING() and GROUPING_ID() supported. Useful for reporting queries that need specific subtotal combinations without the full output of CUBE.
Standard GROUPING SETS syntax since Oracle 8i. Can be combined with ROLLUP and CUBE. GROUPING() and GROUPING_ID() supported.
GROUPING SETS is not supported. Use UNION ALL to combine individual GROUP BY queries for each desired grouping.