

# Aggregate functions in DuckDB

Use aggregate functions to compute one value from many rows, such as a count, a total, an average, or the value from the row with the largest amount. Without a `GROUP BY` clause, an aggregate function reads every row that the query selects and returns one row. With `GROUP BY`, it returns one row for each group, computed over the rows of that group. For more information about grouping, see [`GROUP BY` clause](duckdb-statement-select-group-by.md).

Most aggregate functions skip `NULL` input values. When every input value is `NULL`, or when there are no input rows, they return `NULL`. The `count` and `approx_count_distinct` functions return `0` instead. The `count_if` function returns `NULL`, like most aggregate functions. Some functions, such as `arg_max_null` and `arg_max_nulls_last`, keep `NULL` values on purpose, and their pages say so.

Inside the parentheses of an aggregate function call, `DISTINCT` passes each distinct value to the function once, and `ORDER BY` sets the order in which the function reads its input. Use `ORDER BY` with functions whose result depends on that order, such as `any_value`, `first`, `list`, and `string_agg`. After the call, `FILTER (WHERE {{condition}})` limits the rows that one aggregate reads without changing the rows that the rest of the query reads. For more information about these clauses, see [Aggregate function clauses](duckdb-functions-aggregate-general.md#athena-duckdb-functions-aggregate-aggregate-clauses).

**Topics**
+ [General](duckdb-functions-aggregate-general.md)
+ [Sum and average](duckdb-functions-aggregate-sums-and-averages.md)
+ [Arg max, first](duckdb-functions-aggregate-row-values.md)
+ [Percentiles](duckdb-functions-aggregate-percentiles.md)
+ [Frequency](duckdb-functions-aggregate-frequency-and-histograms.md)
+ [Bool and bitwise](duckdb-functions-aggregate-boolean-and-bitwise.md)
+ [Statistics](duckdb-functions-aggregate-statistics.md)
+ [Regression](duckdb-functions-aggregate-linear-regression.md)

The following table lists the aggregate functions, with a link to the description of each one.


| Function | Description | 
| --- | --- | 
| [Aggregate function clauses](duckdb-functions-aggregate-general.md#athena-duckdb-functions-aggregate-aggregate-clauses) | Describes the `DISTINCT`, `ORDER BY`, and `FILTER` clauses, which change the input rows of an aggregate function call or their order. | 
| [any\_value](duckdb-functions-aggregate-row-values.md#athena-duckdb-functions-aggregate-any-value) | Returns a value from one of the input rows, skipping `NULL` values. | 
| [approx\_count\_distinct](duckdb-functions-aggregate-general.md#athena-duckdb-functions-aggregate-approx-count-distinct) | Returns an estimate of the number of distinct non-`NULL` values. | 
| [approx\_quantile](duckdb-functions-aggregate-percentiles.md#athena-duckdb-functions-aggregate-approx-quantile) | Returns an estimate of a quantile, or of several quantiles, of the input values. | 
| [approx\_top\_k](duckdb-functions-aggregate-frequency-and-histograms.md#athena-duckdb-functions-aggregate-approx-top-k) | Returns an estimate of the `k` most frequent values, as a list. | 
| arbitrary | Alias of [first](duckdb-functions-aggregate-row-values.md#athena-duckdb-functions-aggregate-first). | 
| [arg\_max](duckdb-functions-aggregate-row-values.md#athena-duckdb-functions-aggregate-arg-max) | Returns the value of an expression from the row that has the largest value of another expression. | 
| [arg\_max\_null](duckdb-functions-aggregate-row-values.md#athena-duckdb-functions-aggregate-arg-max-null) | Returns the value of an expression from the row that has the largest value of another expression, even when that value is `NULL`. | 
| [arg\_max\_nulls\_last](duckdb-functions-aggregate-row-values.md#athena-duckdb-functions-aggregate-arg-max-nulls-last) | Returns the value of an expression from the rows with the largest values of another expression, with `NULL` values sorted last. | 
| [arg\_min](duckdb-functions-aggregate-row-values.md#athena-duckdb-functions-aggregate-arg-min) | Returns the value of an expression from the row that has the smallest value of another expression. | 
| [arg\_min\_null](duckdb-functions-aggregate-row-values.md#athena-duckdb-functions-aggregate-arg-min-null) | Returns the value of an expression from the row that has the smallest value of another expression, even when that value is `NULL`. | 
| [arg\_min\_nulls\_last](duckdb-functions-aggregate-row-values.md#athena-duckdb-functions-aggregate-arg-min-nulls-last) | Returns the value of an expression from the rows with the smallest values of another expression, with `NULL` values sorted last. | 
| argmax | Alias of [arg\_max](duckdb-functions-aggregate-row-values.md#athena-duckdb-functions-aggregate-arg-max). | 
| argmin | Alias of [arg\_min](duckdb-functions-aggregate-row-values.md#athena-duckdb-functions-aggregate-arg-min). | 
| array\_agg | Alias of [list](duckdb-functions-aggregate-general.md#athena-duckdb-functions-aggregate-list). | 
| [avg](duckdb-functions-aggregate-sums-and-averages.md#athena-duckdb-functions-aggregate-avg) | Returns the average of the non-`NULL` input values. | 
| [bit\_and](duckdb-functions-aggregate-boolean-and-bitwise.md#athena-duckdb-functions-aggregate-bit-and) | Combines the non-`NULL` input values with a bitwise AND. | 
| [bit\_or](duckdb-functions-aggregate-boolean-and-bitwise.md#athena-duckdb-functions-aggregate-bit-or) | Combines the non-`NULL` input values with a bitwise OR. | 
| [bit\_xor](duckdb-functions-aggregate-boolean-and-bitwise.md#athena-duckdb-functions-aggregate-bit-xor) | Combines the non-`NULL` input values with a bitwise exclusive OR (XOR). | 
| [bitstring\_agg](duckdb-functions-aggregate-boolean-and-bitwise.md#athena-duckdb-functions-aggregate-bitstring-agg) | Returns a `BIT` string that marks which integers in a range occur in the input. | 
| [bool\_and](duckdb-functions-aggregate-boolean-and-bitwise.md#athena-duckdb-functions-aggregate-bool-and) | Tests whether a condition holds for all rows, ignoring `NULL` values. | 
| [bool\_or](duckdb-functions-aggregate-boolean-and-bitwise.md#athena-duckdb-functions-aggregate-bool-or) | Tests whether a condition holds for at least one row, ignoring `NULL` values. | 
| [corr](duckdb-functions-aggregate-statistics.md#athena-duckdb-functions-aggregate-corr) | Returns the correlation coefficient of two sets of numbers. | 
| [count](duckdb-functions-aggregate-general.md#athena-duckdb-functions-aggregate-count) | Counts the rows in a group, or the values that are not `NULL`. | 
| [count\_if](duckdb-functions-aggregate-general.md#athena-duckdb-functions-aggregate-count-if) | Counts the rows for which a condition is true. | 
| countif | Alias of [count\_if](duckdb-functions-aggregate-general.md#athena-duckdb-functions-aggregate-count-if). | 
| [covar\_pop](duckdb-functions-aggregate-statistics.md#athena-duckdb-functions-aggregate-covar-pop) | Returns the population covariance of two sets of numbers. | 
| [covar\_samp](duckdb-functions-aggregate-statistics.md#athena-duckdb-functions-aggregate-covar-samp) | Returns the sample covariance of two sets of numbers. | 
| [decimal\_average](duckdb-functions-aggregate-sums-and-averages.md#athena-duckdb-functions-aggregate-decimal-average) | Returns the average of `DECIMAL` values as a `DECIMAL` of the same type. | 
| [entropy](duckdb-functions-aggregate-statistics.md#athena-duckdb-functions-aggregate-entropy) | Returns the base-2 entropy of the distribution of values in a group. | 
| [favg](duckdb-functions-aggregate-sums-and-averages.md#athena-duckdb-functions-aggregate-favg) | Returns the average of a set of numbers as a `DOUBLE`, and reduces the floating-point rounding error that builds up as the values are added. | 
| [first](duckdb-functions-aggregate-row-values.md#athena-duckdb-functions-aggregate-first) | Returns the value from the first row of a group, even when that value is `NULL`. | 
| fsum | Alias of [kahan\_sum](duckdb-functions-aggregate-sums-and-averages.md#athena-duckdb-functions-aggregate-kahan-sum). | 
| [geomean](duckdb-functions-aggregate-sums-and-averages.md#athena-duckdb-functions-aggregate-geomean) | Returns the geometric mean of a set of positive numbers. | 
| [geometric\_mean](duckdb-functions-aggregate-sums-and-averages.md#athena-duckdb-functions-aggregate-geometric-mean) | Returns the geometric mean of a set of positive numbers, the same as `geomean`. | 
| group\_concat | Alias of [string\_agg](duckdb-functions-aggregate-general.md#athena-duckdb-functions-aggregate-string-agg). | 
| [histogram](duckdb-functions-aggregate-frequency-and-histograms.md#athena-duckdb-functions-aggregate-histogram) | Counts how often each value occurs, or how many values fall into each bin that you give, and returns the counts as a map. | 
| [histogram\_exact](duckdb-functions-aggregate-frequency-and-histograms.md#athena-duckdb-functions-aggregate-histogram-exact) | Counts how many values equal each bin that you give, and returns the counts as a map. | 
| [is\_histogram\_other\_bin](duckdb-functions-aggregate-frequency-and-histograms.md#athena-duckdb-functions-aggregate-is-histogram-other-bin) | Tests whether a value is the key of the extra bin in which `histogram` counts values outside the bins that you list. | 
| [kahan\_sum](duckdb-functions-aggregate-sums-and-averages.md#athena-duckdb-functions-aggregate-kahan-sum) | Returns the sum of a set of numbers as a `DOUBLE`, and reduces the floating-point rounding error that builds up as the values are added. | 
| [kurtosis](duckdb-functions-aggregate-statistics.md#athena-duckdb-functions-aggregate-kurtosis) | Returns the sample excess kurtosis of a set of numbers. | 
| [kurtosis\_pop](duckdb-functions-aggregate-statistics.md#athena-duckdb-functions-aggregate-kurtosis-pop) | Returns the population excess kurtosis of a set of numbers. | 
| [last](duckdb-functions-aggregate-row-values.md#athena-duckdb-functions-aggregate-last) | Returns the value from the last row of a group, in the order that you set with `ORDER BY`. | 
| [list](duckdb-functions-aggregate-general.md#athena-duckdb-functions-aggregate-list) | Collects the values of a group into a list. | 
| listagg | Alias of [string\_agg](duckdb-functions-aggregate-general.md#athena-duckdb-functions-aggregate-string-agg). | 
| [lttb](duckdb-functions-aggregate-general.md#athena-duckdb-functions-aggregate-lttb) | Reduces a series of (`x`, `y`) points to a smaller set of points that keeps the shape of the series. | 
| [mad](duckdb-functions-aggregate-statistics.md#athena-duckdb-functions-aggregate-mad) | Returns the median absolute deviation of a set of values. | 
| [max](duckdb-functions-aggregate-general.md#athena-duckdb-functions-aggregate-max) | Returns the largest value in a group, or a list of the largest values. | 
| max\_by | Alias of [arg\_max](duckdb-functions-aggregate-row-values.md#athena-duckdb-functions-aggregate-arg-max). | 
| mean | Alias of [avg](duckdb-functions-aggregate-sums-and-averages.md#athena-duckdb-functions-aggregate-avg). | 
| [median](duckdb-functions-aggregate-percentiles.md#athena-duckdb-functions-aggregate-median) | Returns the median of a group, the value in the middle when the values are sorted. | 
| [min](duckdb-functions-aggregate-general.md#athena-duckdb-functions-aggregate-min) | Returns the smallest value in a group, or a list of the smallest values. | 
| min\_by | Alias of [arg\_min](duckdb-functions-aggregate-row-values.md#athena-duckdb-functions-aggregate-arg-min). | 
| [mode](duckdb-functions-aggregate-frequency-and-histograms.md#athena-duckdb-functions-aggregate-mode) | Returns the value that occurs most often in a group. | 
| [product](duckdb-functions-aggregate-sums-and-averages.md#athena-duckdb-functions-aggregate-product) | Multiplies the values in a group and returns the product. | 
| quantile | Alias of [quantile\_disc](duckdb-functions-aggregate-percentiles.md#athena-duckdb-functions-aggregate-quantile-disc). | 
| [quantile\_cont](duckdb-functions-aggregate-percentiles.md#athena-duckdb-functions-aggregate-quantile-cont) | Returns an interpolated quantile, such as a percentile, of a set of values. | 
| [quantile\_disc](duckdb-functions-aggregate-percentiles.md#athena-duckdb-functions-aggregate-quantile-disc) | Returns a quantile, such as a percentile, of a set of values, chosen from the values themselves. | 
| [regr\_avgx](duckdb-functions-aggregate-linear-regression.md#athena-duckdb-functions-aggregate-regr-avgx) | Averages the independent variable `x`, using only the rows where neither argument is `NULL`. | 
| [regr\_avgy](duckdb-functions-aggregate-linear-regression.md#athena-duckdb-functions-aggregate-regr-avgy) | Averages the dependent variable `y`, using only the rows where neither argument is `NULL`. | 
| [regr\_count](duckdb-functions-aggregate-linear-regression.md#athena-duckdb-functions-aggregate-regr-count) | Returns the number of rows where neither the dependent nor the independent variable is `NULL`. | 
| [regr\_intercept](duckdb-functions-aggregate-linear-regression.md#athena-duckdb-functions-aggregate-regr-intercept) | Fits a least-squares line to the (`x`, `y`) pairs in a group and returns its y-intercept. | 
| [regr\_r2](duckdb-functions-aggregate-linear-regression.md#athena-duckdb-functions-aggregate-regr-r2) | Fits a least-squares line to the (`x`, `y`) pairs in a group and returns its coefficient of determination (R squared), which shows how well the line fits. | 
| [regr\_slope](duckdb-functions-aggregate-linear-regression.md#athena-duckdb-functions-aggregate-regr-slope) | Fits a least-squares line to the (`x`, `y`) pairs in a group and returns its slope. | 
| [regr\_sxx](duckdb-functions-aggregate-linear-regression.md#athena-duckdb-functions-aggregate-regr-sxx) | Returns the sum of the squared deviations of the independent variable from its mean, over the rows where neither variable is `NULL`. | 
| [regr\_sxy](duckdb-functions-aggregate-linear-regression.md#athena-duckdb-functions-aggregate-regr-sxy) | Returns the sum of the products of the deviations of two variables from their means, over the rows where neither variable is `NULL`. | 
| [regr\_syy](duckdb-functions-aggregate-linear-regression.md#athena-duckdb-functions-aggregate-regr-syy) | Returns the sum of the squared deviations of the dependent variable from its mean, over the rows where neither variable is `NULL`. | 
| [reservoir\_quantile](duckdb-functions-aggregate-percentiles.md#athena-duckdb-functions-aggregate-reservoir-quantile) | Estimates a quantile of the values in a group from a random sample of the values. | 
| [sem](duckdb-functions-aggregate-statistics.md#athena-duckdb-functions-aggregate-sem) | Computes the standard error of the mean for the values in a group. | 
| [skewness](duckdb-functions-aggregate-statistics.md#athena-duckdb-functions-aggregate-skewness) | Measures how asymmetric the distribution of the values in a group is. | 
| stddev | Alias of [stddev\_samp](duckdb-functions-aggregate-statistics.md#athena-duckdb-functions-aggregate-stddev-samp). | 
| [stddev\_pop](duckdb-functions-aggregate-statistics.md#athena-duckdb-functions-aggregate-stddev-pop) | Computes the population standard deviation of the values in a group. | 
| [stddev\_samp](duckdb-functions-aggregate-statistics.md#athena-duckdb-functions-aggregate-stddev-samp) | Computes the sample standard deviation of the values in a group. | 
| [string\_agg](duckdb-functions-aggregate-general.md#athena-duckdb-functions-aggregate-string-agg) | Joins the non-`NULL` values in a group into one string, with a separator between them. | 
| [sum](duckdb-functions-aggregate-sums-and-averages.md#athena-duckdb-functions-aggregate-sum) | Adds up the non-`NULL` values in a group. | 
| sumkahan | Alias of [kahan\_sum](duckdb-functions-aggregate-sums-and-averages.md#athena-duckdb-functions-aggregate-kahan-sum). | 
| [var\_pop](duckdb-functions-aggregate-statistics.md#athena-duckdb-functions-aggregate-var-pop) | Computes the population variance of the values in a group. | 
| [var\_samp](duckdb-functions-aggregate-statistics.md#athena-duckdb-functions-aggregate-var-samp) | Computes the sample variance of the values in a group. | 
| variance | Alias of [var\_samp](duckdb-functions-aggregate-statistics.md#athena-duckdb-functions-aggregate-var-samp). | 
| wavg | Alias of [weighted\_avg](duckdb-functions-aggregate-sums-and-averages.md#athena-duckdb-functions-aggregate-weighted-avg). | 
| [weighted\_avg](duckdb-functions-aggregate-sums-and-averages.md#athena-duckdb-functions-aggregate-weighted-avg) | Returns the average of the values in a group, with each value weighted by a second column. | 