GROUPING SETS vs ROLLUP vs CUBE in SQL: what groupings does each produce, and when do you use each?

Quick Overview

Asks what GROUPING SETS, ROLLUP and CUBE do in SQL, which aggregate groupings each one produces, and when to use each. Tests understanding of subtotals and grand totals, how the number of groupings grows with the column list, and how to label subtotal rows in the result.

GROUPING SETS vs ROLLUP vs CUBE in SQL: what groupings does each produce, and when do you use each?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Explain the purpose of `GROUPING SETS`, `ROLLUP`, and `CUBE`. How do they differ in the aggregate groupings they produce, and when would you use each? ```hint List the groupings For a two-column list such as `(a, b)`, write out the column subsets each clause groups by, then work out how that count grows with more columns. ``` ```hint Read the output Think about how someone reading the result tells a subtotal row apart from a row whose column value really is NULL. ``` ### What a Strong Answer Covers - What all three add to a plain `GROUP BY` - The exact groupings each produces for a small column list, and how the number of groupings grows with the number of columns - Why column order matters for one of them and not for the others - How subtotal and grand total rows are identified in the output - Which reporting situations call for each ### Follow-up Questions - Rewrite `ROLLUP(a, b, c)` and `CUBE(a, b)` as `GROUPING SETS`. - A grouping column contains real NULL values. How do you tell those rows apart from subtotal rows? - What groupings does `GROUP BY a, ROLLUP(b, c)` produce? - Why is one query with `GROUPING SETS` usually cheaper than a `UNION ALL` of separate `GROUP BY` queries?

Overview: Asks what GROUPING SETS, ROLLUP and CUBE do in SQL, which aggregate groupings each one produces, and when to use each. Tests understanding of subtotals and grand totals, how the number of groupings grows with the column list, and how to label subtotal rows in the result.

General logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Explain the purpose of GROUPING SETS, ROLLUP, and CUBE. How do they differ in the aggregate groupings they produce, and when would you use each?

What a Strong Answer Covers Guidance

  • What all three add to a plain GROUP BY
  • The exact groupings each produces for a small column list, and how the number of groupings grows with the number of columns
  • Why column order matters for one of them and not for the others
  • How subtotal and grand total rows are identified in the output
  • Which reporting situations call for each

Follow-up Questions Guidance

  • Rewrite ROLLUP(a, b, c) and CUBE(a, b) as GROUPING SETS .
  • A grouping column contains real NULL values. How do you tell those rows apart from subtotal rows?
  • What groupings does GROUP BY a, ROLLUP(b, c) produce?
  • Why is one query with GROUPING SETS usually cheaper than a UNION ALL of separate GROUP BY queries?