SQL Flow Field Guide

GROUP BY CUBE

Catalog support for SQL Server 2022 (16.x) and 2025 (17.x)

cross-tab super-aggregates (2^n grouping sets)

What changes

Input
rows after WHERE
Output or change
groups for every combination of the key columns

Syntax at a glance

GROUP BY CUBE ( column_expression [ ,...n ] )

Work example

An analysis needs every subtotal combination across a small set of grouping columns.

Use two dimensions in a disposable query. Distinguish rolled-up NULL values with GROUPING or GROUPING_ID.

Check your work GROUP BY CUBE. Detail rows and each subtotal level appear once and rolled-up values are labeled clearly.

Open Microsoft Learn for full syntax, permissions, and details for your version.