PostgreSQL Aggregate Functions
PostgreSQL aggregate functions are mainly used for data statistics, such as count, sum, average, etc. Aggregate functions combined with grouping operations allow you to count data in more dimensions. For JSON array and object aggregates and their cross-database differences, see SQL JSON aggregation by database.
-
array_agg
PostgreSQL array_agg() collects input values into an array, including NULL values. Add ORDER BY inside the aggregate to control element order. -
avg
The PostgreSQLavg()function is an aggregate function that computes the average of all specified values in a group and returns it. -
bit_and
The PostgreSQLbit_and()function is an aggregate function that performs a “bitwise AND” operation on all non-null input values. -
bit_or
The PostgreSQLbit_or()function is an aggregate function that performs a “bitwise OR” of all non-null input values. -
bit_xor
The PostgreSQLbit_xor()function is an aggregate function that performs a “bitwise XOR” operation on all non-null input values. -
bool_and
The PostgreSQLbool_and()function is an aggregate function that returns the “logical AND” of all specified non-null Boolean values in a group. -
bool_or
The PostgreSQLbool_or()function is an aggregate function that returns the “logical OR” of all specified non-null Boolean values in a group. -
count
The PostgreSQLcount()function is an aggregate function that counts and returns the number of all specified values in a group. -
every
The PostgreSQLevery()function is an aggregate function that returns the result of the “logical AND” of all specified non-null Boolean values in a group. -
json_agg
PostgreSQL json_agg() collects input values, including NULL, into a JSON array. Use ORDER BY inside the aggregate to control element order. -
json_object_agg
PostgreSQL json_object_agg() builds a json object from key-value rows. Keys cannot be NULL; values may be NULL and become JSON null. -
jsonb_agg
PostgreSQL jsonb_agg() collects input values, including NULL, into a JSONB array. Use ORDER BY inside the aggregate to control element order. -
jsonb_object_agg
PostgreSQL jsonb_object_agg() returns jsonb from key-value rows. Keys cannot be NULL; values may be NULL, and duplicate-key behavior depends on input order. -
max
The PostgreSQLmax()function is an aggregate function that returns the maximum value of all specified values in a group. -
min
The PostgreSQLmin()function is an aggregate function that returns the minimum value of all specified values in a group. -
range_agg
The PostgreSQLrange_agg()function is an aggregate function that returns the union of all non-NULL input range values in a group. -
range_intersect_agg
The PostgreSQLrange_intersect_agg()function is an aggregate function that returns the intersection of all non-NULL input range values in a group. -
stddev
The PostgreSQLstddev()function is an aggregate function that computes the sample standard deviation of all non-null input values. -
stddev_pop
The PostgreSQLstddev_pop()function is an aggregate function that computes the population standard deviation of all non-null input values. -
stddev_samp
The PostgreSQLstddev_samp()function is an aggregate function that computes the sample standard deviation of all non-null input values. -
string_agg
PostgreSQL string_agg() concatenates non-NULL text values with a delimiter. Use ORDER BY inside the aggregate to control the output order. -
sum
The PostgreSQLsum()function is an aggregate function that computes the sum of all specified values in a group and returns the result. -
var_pop
The PostgreSQLvar_pop()function is an aggregate function that computes the population variance (square of the population standard deviation) for all non-null input values. -
var_samp
The PostgreSQLvar_samp()function is an aggregate function that computes the sample variance (square of the sample standard deviation) for all non-null input values. -
variance
The PostgreSQLvariance()function is an aggregate function that computes the sample variance (square of the sample standard deviation) for all non-null input values.