clause

GROUPING SETS

Computes multiple grouping levels in one query. The empty grouping set produces the grand total. GROUPING identifies subtotal rows. Example target: PostgreSQL 17. Use only the isolated starter dataset; setup and expected results are in the batch evidence.

Formal Syntax & Structure

sql syntaxsql
GROUP BY GROUPING SETS ((column), ())

Working Example

Example Snippetsql
SELECT customer_id, SUM(amount) FROM orders GROUP BY GROUPING SETS ((customer_id), ()) ORDER BY GROUPING(customer_id), customer_id;
SQL Reference