IT lexicon › Database › GROUPING SETS

GROUPING SETS

Database På svenska → Updated: 2026-05-28

A SQL extension of GROUP BY that lets you compute several different groupings in a single query. Instead of running several SELECTs with UNION, you get e.g. totals per country, per product, AND the grand total at once.

Syntax: GROUP BY GROUPING SETS ((country), (product), ()) → a result set with three levels of aggregation. The empty set () gives the grand total. The GROUPING() function tells which level a row belongs to (to distinguish "NULL = grand total" from a genuine NULL value). Use case: reports and dashboards that need subtotals across several dimensions at once. ROLLUP and CUBE are shortcuts: ROLLUP gives hierarchical subtotals, CUBE gives all combinations. A SQL:1999 standard, supported by PostgreSQL, SQL Server, Oracle, MySQL 8. Much more efficient than reading the table several times with separate aggregations.

← Back to the lexicon