Aggregate functions operate on a set of values to compute a single result.
array_aggReturns an array created from the expression elements. If ordering is required, elements are inserted in the specified order.
array_agg(expression [ORDER BY expression])
> SELECT array_agg(column_name ORDER BY other_column) FROM table_name; +-----------------------------------------------+ | array_agg(column_name ORDER BY other_column) | +-----------------------------------------------+ | [element1, element2, element3] | +-----------------------------------------------+
avgReturns the average of numeric values in the specified column.
avg(expression)
> SELECT avg(column_name) FROM table_name; +---------------------------+ | avg(column_name) | +---------------------------+ | 42.75 | +---------------------------+
bit_andComputes the bitwise AND of all non-null input values.
bit_and(expression)
bit_orComputes the bitwise OR of all non-null input values.
bit_or(expression)
bit_xorComputes the bitwise exclusive OR of all non-null input values.
bit_xor(expression)
bool_andReturns true if all non-null input values are true, otherwise false.
bool_and(expression)
> SELECT bool_and(column_name) FROM table_name; +----------------------------+ | bool_and(column_name) | +----------------------------+ | true | +----------------------------+
bool_orReturns true if all non-null input values are true, otherwise false.
bool_and(expression)
> SELECT bool_and(column_name) FROM table_name; +----------------------------+ | bool_and(column_name) | +----------------------------+ | true | +----------------------------+
countReturns the number of non-null values in the specified column. To include null values in the total count, use count(*).
count(expression)
> SELECT count(column_name) FROM table_name; +-----------------------+ | count(column_name) | +-----------------------+ | 100 | +-----------------------+ > SELECT count(*) FROM table_name; +------------------+ | count(*) | +------------------+ | 120 | +------------------+
first_valueReturns the first element in an aggregation group according to the requested ordering. If no ordering is given, returns an arbitrary element from the group.
first_value(expression [ORDER BY expression])
> SELECT first_value(column_name ORDER BY other_column) FROM table_name; +-----------------------------------------------+ | first_value(column_name ORDER BY other_column)| +-----------------------------------------------+ | first_element | +-----------------------------------------------+
groupingReturns 1 if the data is aggregated across the specified column, or 0 if it is not aggregated in the result set.
grouping(expression)
> SELECT column_name, GROUPING(column_name) AS group_column FROM table_name GROUP BY GROUPING SETS ((column_name), ()); +-------------+-------------+ | column_name | group_column | +-------------+-------------+ | value1 | 0 | | value2 | 0 | | NULL | 1 | +-------------+-------------+
last_valueReturns the last element in an aggregation group according to the requested ordering. If no ordering is given, returns an arbitrary element from the group.
last_value(expression [ORDER BY expression])
> SELECT last_value(column_name ORDER BY other_column) FROM table_name; +-----------------------------------------------+ | last_value(column_name ORDER BY other_column) | +-----------------------------------------------+ | last_element | +-----------------------------------------------+
maxReturns the maximum value in the specified column.
max(expression)
> SELECT max(column_name) FROM table_name; +----------------------+ | max(column_name) | +----------------------+ | 150 | +----------------------+
meanAlias of avg.
medianReturns the median value in the specified column.
median(expression)
> SELECT median(column_name) FROM table_name; +----------------------+ | median(column_name) | +----------------------+ | 45.5 | +----------------------+
minReturns the minimum value in the specified column.
min(expression)
> SELECT min(column_name) FROM table_name; +----------------------+ | min(column_name) | +----------------------+ | 12 | +----------------------+
string_aggConcatenates the values of string expressions and places separator values between them.
string_agg(expression, delimiter)
> SELECT string_agg(name, ', ') AS names_list FROM employee; +--------------------------+ | names_list | +--------------------------+ | Alice, Bob, Charlie | +--------------------------+
sumReturns the sum of all values in the specified column.
sum(expression)
> SELECT sum(column_name) FROM table_name; +-----------------------+ | sum(column_name) | +-----------------------+ | 12345 | +-----------------------+
varReturns the statistical sample variance of a set of numbers.
var(expression)
var_popReturns the statistical population variance of a set of numbers.
var_pop(expression)
var_populationAlias of var_pop.
var_sampAlias of var.
var_sampleAlias of var.
corrReturns the coefficient of correlation between two numeric values.
corr(expression1, expression2)
> SELECT corr(column1, column2) FROM table_name; +--------------------------------+ | corr(column1, column2) | +--------------------------------+ | 0.85 | +--------------------------------+
covarAlias of covar_samp.
covar_popReturns the sample covariance of a set of number pairs.
covar_samp(expression1, expression2)
> SELECT covar_samp(column1, column2) FROM table_name; +-----------------------------------+ | covar_samp(column1, column2) | +-----------------------------------+ | 8.25 | +-----------------------------------+
covar_sampReturns the sample covariance of a set of number pairs.
covar_samp(expression1, expression2)
> SELECT covar_samp(column1, column2) FROM table_name; +-----------------------------------+ | covar_samp(column1, column2) | +-----------------------------------+ | 8.25 | +-----------------------------------+
nth_valueReturns the nth value in a group of values.
nth_value(expression, n ORDER BY expression)
> SELECT dept_id, salary, NTH_VALUE(salary, 2) OVER (PARTITION BY dept_id ORDER BY salary ASC) AS second_salary_by_dept FROM employee; +---------+--------+-------------------------+ | dept_id | salary | second_salary_by_dept | +---------+--------+-------------------------+ | 1 | 30000 | NULL | | 1 | 40000 | 40000 | | 1 | 50000 | 40000 | | 2 | 35000 | NULL | | 2 | 45000 | 45000 | +---------+--------+-------------------------+
regr_avgxComputes the average of the independent variable (input) expression_x for the non-null paired data points.
regr_avgx(expression_y, expression_x)
regr_avgyComputes the average of the dependent variable (output) expression_y for the non-null paired data points.
regr_avgy(expression_y, expression_x)
regr_countCounts the number of non-null paired data points.
regr_count(expression_y, expression_x)
regr_interceptComputes the y-intercept of the linear regression line. For the equation (y = kx + b), this function returns b.
regr_intercept(expression_y, expression_x)
regr_r2Computes the square of the correlation coefficient between the independent and dependent variables.
regr_r2(expression_y, expression_x)
regr_slopeReturns the slope of the linear regression line for non-null pairs in aggregate columns. Given input column Y and X: regr_slope(Y, X) returns the slope (k in Y = k*X + b) using minimal RSS fitting.
regr_slope(expression_y, expression_x)
regr_sxxComputes the sum of squares of the independent variable.
regr_sxx(expression_y, expression_x)
regr_sxyComputes the sum of products of paired data points.
regr_sxy(expression_y, expression_x)
regr_syyComputes the sum of squares of the dependent variable.
regr_syy(expression_y, expression_x)
stddevReturns the standard deviation of a set of numbers.
stddev(expression)
> SELECT stddev(column_name) FROM table_name; +----------------------+ | stddev(column_name) | +----------------------+ | 12.34 | +----------------------+
stddev_popReturns the population standard deviation of a set of numbers.
stddev_pop(expression)
> SELECT stddev_pop(column_name) FROM table_name; +--------------------------+ | stddev_pop(column_name) | +--------------------------+ | 10.56 | +--------------------------+
stddev_sampAlias of stddev.
approx_distinctReturns the approximate number of distinct input values calculated using the HyperLogLog algorithm.
approx_distinct(expression)
> SELECT approx_distinct(column_name) FROM table_name; +-----------------------------------+ | approx_distinct(column_name) | +-----------------------------------+ | 42 | +-----------------------------------+
approx_medianReturns the approximate median (50th percentile) of input values. It is an alias of approx_percentile_cont(x, 0.5).
approx_median(expression)
> SELECT approx_median(column_name) FROM table_name; +-----------------------------------+ | approx_median(column_name) | +-----------------------------------+ | 23.5 | +-----------------------------------+
approx_percentile_contReturns the approximate percentile of input values using the t-digest algorithm.
approx_percentile_cont(expression, percentile, centroids)
> SELECT approx_percentile_cont(column_name, 0.75, 100) FROM table_name; +-------------------------------------------------+ | approx_percentile_cont(column_name, 0.75, 100) | +-------------------------------------------------+ | 65.0 | +-------------------------------------------------+
approx_percentile_cont_with_weightReturns the weighted approximate percentile of input values using the t-digest algorithm.
approx_percentile_cont_with_weight(expression, weight, percentile)
> SELECT approx_percentile_cont_with_weight(column_name, weight_column, 0.90) FROM table_name; +----------------------------------------------------------------------+ | approx_percentile_cont_with_weight(column_name, weight_column, 0.90) | +----------------------------------------------------------------------+ | 78.5 | +----------------------------------------------------------------------+