GROUPING SETS

SQL · Grouping Rows

Theory

GROUPING SETS lets one query aggregate at several levels at once. You list each grouping you want in brackets, and the engine returns the union of all of them. GROUP BY GROUPING SETS ((dept), (role), ()) gives a count per department, a count per role and one grand total row, all in a single result.

In rows produced by a grouping that does not include a column, that column shows NULL, meaning "all values". To tell such a subtotal NULL apart from a real NULL in the data, use the GROUPING(col) function, which returns 1 when the column was rolled up. GROUPING SETS is supported by PostgreSQL, SQL Server, Oracle, Snowflake and BigQuery; MySQL and SQLite lack it, so there you write one GROUP BY per level and combine them with UNION ALL.

Why we use it

Reports often need totals at several levels side by side. Without GROUPING SETS you write several GROUP BY queries and UNION ALL them, which is longer and scans the table once per query.

When to use it

Use GROUPING SETS when you need specific, hand-picked aggregation levels. For a strict hierarchy of subtotals use ROLLUP; for every combination use CUBE. On MySQL or SQLite, emulate it with UNION ALL.

How it works

  1. Read the GROUPING SETS list
  2. Take the next grouping set, e.g. (dept)
  3. Group rows by those columns and aggregate
  4. Fill columns not in this set with NULL
  5. More grouping sets left? Yes: Process the next set. No: Combine all results.
  6. Return one combined result

Syntax

SELECT c1, c2, AGG(c3)
FROM table
GROUP BY GROUPING SETS ((c1), (c2), ());

Example

SELECT department, role, COUNT(*)
FROM employees
GROUP BY GROUPING SETS ((department), (role), ());

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic