Skip to main content
ReferenceApplies to: Unified engine deployments
Aggregate functions reduce rows to a value per group. Every selected expression must be grouped or aggregated, apart from supported constant expressions.

GROUP BY

GROUP BY creates one row for each distinct grouping key. NULL grouping keys form a group. WHERE filters source rows before grouping.

HAVING

HAVING filters groups after aggregation. Use WHERE for input predicates that should not affect all groups equally.

ROLLUP, CUBE, and grouping sets

ROLLUP produces hierarchical subtotals. CUBE produces all combinations of grouping keys. GROUPING SETS enumerates the combinations explicitly. Subtotal rows use NULL in omitted dimensions; use GROUPING or GROUPING_ID where supported to distinguish omitted keys from actual null values.

Counts and empty inputs

COUNT(*) counts rows, including rows with null fields. COUNT(expression) counts non-null values. COUNT(DISTINCT expression) counts distinct non-null values. SUM, AVG, MIN, and MAX ignore null input values. An aggregate query without GROUP BY still produces a row on empty input; COUNT is zero and most value aggregates are NULL. A grouped query with no input has no groups.
See aggregate functions for signatures and statistical aggregates. collect_list and collect_set are unavailable in Unified.
Last modified on November 4, 2026