SQL Flow Field Guide

GROUP BY GROUPING SETS

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

computes several GROUP BY results in one pass and unions them

What changes

Input
rows after WHERE
Output or change
union of the listed groupings

Syntax at a glance

GROUP BY GROUPING SETS ( <grouping_set> [ ,...n ] )

Work example

One report needs a chosen set of totals without computing every possible subtotal combination.

List each grouping set and compare its total with a separate simple GROUP BY query.

Check your work GROUP BY GROUPING SETS. Every requested grouping appears once and no unrequested subtotal is returned.

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