Request a demo

BigQuery · Query syntax

BigQuery GROUP BY ROLLUP, CUBE and GROUPING SETS

ROLLUP, CUBE and GROUPING SETS compute several GROUP BY levels in one query: ROLLUP(a, b) adds subtotals per a and a grand total, CUBE(a, b) adds every combination, and GROUPING SETS lists the exact groupings you want. GROUPING() tells subtotal rows from real NULLs.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

GROUP BY ROLLUP (a, b, c) produces the groupings (a, b, c), (a, b), (a) and (): detail rows, subtotals at each level of the hierarchy, and a grand total. GROUP BY CUBE (a, b) produces all combinations: (a, b), (a), (b) and (). GROUP BY GROUPING SETS ((a, b), (a), ()) produces exactly the groupings you list, and can include ROLLUP and CUBE inside it. The result is the same as running one GROUP BY per grouping set and combining them with UNION ALL, but the table is read once.

In a row produced by a coarser grouping, the columns that were rolled up are NULL. That NULL is a placeholder, and it is indistinguishable from a real NULL value in the data unless you call GROUPING(column), which returns 1 when the column was aggregated away in that row and 0 when it was grouped. Use it to label subtotal rows (IF(GROUPING(region) = 1, 'All regions', region)) or to filter to the levels you want with HAVING.

Order matters for ROLLUP (it rolls up from right to left) but not for CUBE. Limits: ROLLUP takes up to 4,095 groupable items and CUBE up to 12, both equivalent to 4,096 grouping sets, from at most 50 distinct columns. GROUP BY () on its own means a grand total, the same as aggregating with no GROUP BY. Ordinals (ROLLUP (1, 2)) and SELECT aliases work in these clauses as in ordinary GROUP BY.

Syntax

GROUP BY ROLLUP (expression[, ...])

GROUP BY CUBE (expression[, ...])

GROUP BY GROUPING SETS (grouping_set[, ...])

grouping_set:
  expression | (expression[, ...]) | () | ROLLUP (...) | CUBE (...)

GROUPING(expression)  -- 1 if aggregated away in this row, else 0

Arguments and return value

ArgumentDescription
ROLLUP (a, b, ...)Groupings for each prefix of the list plus the grand total: (a, b), (a), ().
CUBE (a, b, ...)Groupings for every subset of the list, including the grand total. At most 12 items.
GROUPING SETS (...)An explicit list of groupings. () is the grand total.
GROUPING(expr)Aggregate function returning INT64 1 when expr is a rolled-up placeholder in the current row, 0 otherwise.

Returns: One row per group of every grouping set, with NULL placeholders in the columns that a grouping set does not include.

Examples

Subtotals and a grand total with ROLLUP

WITH sales AS (
  SELECT 'EMEA' AS region, 'DE' AS country, 100 AS revenue UNION ALL
  SELECT 'EMEA', 'FR', 60 UNION ALL
  SELECT 'AMER', 'US', 250
)
SELECT region, country, SUM(revenue) AS revenue
FROM sales
GROUP BY ROLLUP (region, country)
ORDER BY region NULLS LAST, country NULLS LAST;
Result
regioncountryrevenue
AMERUS250
AMERNULL250
EMEADE100
EMEAFR60
EMEANULL160
NULLNULL410

Label subtotal rows with GROUPING

The second order has a real NULL channel. GROUPING separates it from the subtotal placeholder.

WITH orders AS (
  SELECT 'web' AS channel, 40 AS amount UNION ALL
  SELECT CAST(NULL AS STRING), 15 UNION ALL
  SELECT 'app', 25
)
SELECT
  CASE
    WHEN GROUPING(channel) = 1 THEN 'TOTAL'
    ELSE IFNULL(channel, '(no channel)')
  END AS channel_label,
  SUM(amount) AS amount
FROM orders
GROUP BY ROLLUP (channel)
ORDER BY GROUPING(channel), amount DESC;
Result
channel_labelamount
web40
app25
(no channel)15
TOTAL80

Exactly the groupings a report needs with GROUPING SETS

Totals per product and per month in one pass, without the product-by-month detail that CUBE would add.

WITH sales AS (
  SELECT 'pro' AS plan, '2026-10' AS month, 300 AS mrr UNION ALL
  SELECT 'pro', '2026-11', 320 UNION ALL
  SELECT 'team', '2026-11', 900
)
SELECT plan, month, SUM(mrr) AS mrr
FROM sales
GROUP BY GROUPING SETS (plan, month)
ORDER BY plan NULLS LAST, month NULLS LAST;
Result
planmonthmrr
proNULL620
teamNULL900
NULL2026-10300
NULL2026-111220

Common mistakes

  • Treating every NULL as a subtotal. Real NULLs in the data look the same as rolled-up placeholders; test GROUPING(col) = 1 instead of col IS NULL.
  • Expecting ROLLUP order not to matter. ROLLUP (country, region) produces subtotals per country, not per region; list columns from the coarsest to the finest level.
  • Using CUBE when you need a few levels. CUBE over n columns produces 2^n groupings, which multiplies output rows and work; list the levels with GROUPING SETS.
  • Porting GROUP BY a, b WITH ROLLUP from MySQL. BigQuery uses GROUP BY ROLLUP (a, b).
  • Aliasing an expression with the name of a grouped column, such as CASE WHEN GROUPING(channel) = 1 ... END AS channel. GROUP BY then resolves to the alias and fails with "Column channel contains an aggregation function, which is not allowed in GROUP BY"; pick a new alias.

Cost and performance

  • The table is scanned once for all grouping sets, so on-demand cost is the same as one GROUP BY. That is cheaper than a UNION ALL of separate aggregations, where each branch reads and bills the table again.
  • Slot usage grows with the number of grouping sets, because every input row is aggregated once per set. A CUBE over many high-cardinality columns can be much heavier than the scan suggests.

FAQ

How do I add a total row in BigQuery?

GROUP BY ROLLUP (col) adds a grand total row with NULL in col. Label it with IF(GROUPING(col) = 1, 'Total', col).

What is the difference between ROLLUP and CUBE?

ROLLUP follows the column order as a hierarchy: (a, b), (a), (). CUBE produces every combination: (a, b), (a), (b), ().

What does GROUPING() return?

1 if the column was aggregated away in that row (it holds a subtotal placeholder), 0 if the row is grouped by that column.

Errors you may hit

More query syntax

All BigQuery query syntax · All statements and clauses · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.