Aggregate functions
On this page
Aggregate functions summarize rows into one value per group. Use them in an AGGREGATE clause, optionally preceded by GROUP BY.
- Without
GROUP BY, the query returns one row, even for empty input. - With
GROUP BY, the query returns one row per group. Empty input produces no rows.
Each function documents how it handles NULL values and empty input.
Use WITHIN GROUP ORDER BY before AGGREGATE for order-sensitive functions such as first, last, and array_agg. An ORDER BY after AGGREGATE sorts the result rows, not the inputs to each aggregate.