ROLLUP

SQL · Grouping Rows

Theory

ROLLUP is a shorthand for a hierarchy of grouping sets. GROUP BY ROLLUP (dept, role) produces the groupings (dept, role), (dept) and (): detail rows for every department and role, a subtotal row for each department, and one grand total. With n columns it creates n + 1 levels, rolling up from right to left.

Column order matters because ROLLUP assumes the first column is the top of the hierarchy, like year then month or country then city. Subtotal rows show NULL in the rolled-up columns; use GROUPING(col) to label them as 'Total'. PostgreSQL, SQL Server, Oracle and Snowflake support ROLLUP (...); MySQL writes it as GROUP BY dept, role WITH ROLLUP; SQLite has no ROLLUP, so you emulate it with UNION ALL.

Why we use it

Financial and sales reports need subtotals per category plus a grand total, exactly like a spreadsheet with subtotal rows. ROLLUP produces them in one pass instead of several queries.

When to use it

Use ROLLUP when your grouping columns form a natural hierarchy (year > month, region > city). If you need every combination, such as per role across all departments, use CUBE; for hand-picked levels use GROUPING SETS.

How it works

  1. GROUP BY ROLLUP (dept, role)
  2. Group by (dept, role): detail rows
  3. Group by (dept): one subtotal per department
  4. Group by (): one grand total row
  5. Mark rolled-up columns as NULL
  6. Return details, subtotals and the total together

Syntax

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

Example

SELECT department, role, COUNT(*)
FROM employees
GROUP BY ROLLUP (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