Statistical aggregate functions
Use these functions to measure the spread and shape of the values in a group, such as their variance, skewness, or entropy, and how two sets of numbers vary together. Functions whose names end in _pop treat the values as a whole population, and those that end in _samp treat them as a sample.
corr function
Returns the Pearson correlation coefficient of y and x, a value from -1 to 1 that measures how closely the pairs of values fit a straight line. A result of 1 means that y rises exactly in step with x, and -1 means that y falls exactly as x rises. A result of 0 means that there is no linear relationship. Over the rows where both y and x are not NULL, the result equals covar_pop(y, x) / (stddev_pop(x) * stddev_pop(y)).
The function uses only the rows where both y and x are not NULL. If there are no such rows, the result is NULL.
Syntax
corr(y, x)
Arguments
The corr function takes the following arguments:
y-
The dependent values, as
DOUBLE. Other numeric types are converted toDOUBLE. x-
The independent values, as
DOUBLE. Other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query returns the correlation coefficient of four pairs of values. The values of y rise almost in a straight line as x rises, so the result is close to 1.
SELECT corr(y, x) AS result FROM (VALUES (1.0, 2.0), (2.0, 3.5), (3.0, 7.0), (4.0, 8.5) ) AS v(x, y)
The query returns the following result.
| result |
|---|
|
The following SQL query skips the two rows that have a NULL. The remaining pairs lie on the line y = 2 * x, so the result is 1.0.
SELECT corr(y, x) AS result FROM (VALUES (1, 2), (2, 4), (3, NULL), (NULL, 1), (4, 8) ) AS v(x, y)
The query returns the following result.
| result |
|---|
|
The following SQL query shows that the result is NaN when y has the same value in every row.
SELECT isnan(corr(y, x)) AS is_nan FROM (VALUES (1, 2), (2, 2), (3, 2)) AS v(x, y)
The query returns the following result.
| is_nan |
|---|
|
Considerations
If y or x has the same value in every row, or if only one row has both values, the result is NaN, not NULL. To test for this case, use isnan(corr(.y, x))
Differences from Athena engine version 3
In Athena engine version 3, corr of integer columns returns REAL, which keeps about 7 significant digits. In DuckDB, corr returns DOUBLE, so results can differ after the seventh digit.
In Athena engine version 3, corr returns NULL when y or x has the same value in every row. In DuckDB, it returns NaN. To get the Athena engine version 3 result, use nullif(corr(.y, x), 'NaN'::DOUBLE)
covar_pop function
Returns the population covariance of y and x, which is (sum(x * y) - sum(x) * sum(y) / . In this formula, n) / n is the number of rows where both values are not nNULL, and each sum covers only those rows. A positive result means that the two values tend to rise together, and a negative result means that one tends to fall as the other rises.
The function uses only the rows where both y and x are not NULL. If there are no such rows, the result is NULL. If there is one such row, the result is 0. To estimate the covariance of a larger population from a sample, use the covar_samp function.
Syntax
covar_pop(y, x)
Arguments
The covar_pop function takes the following arguments:
y-
The first set of values, as
DOUBLE. Other numeric types are converted toDOUBLE. x-
The second set of values, as
DOUBLE. Other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query returns the population covariance of four pairs of values. The result is positive because y tends to rise as x rises.
SELECT covar_pop(y, x) AS result FROM (VALUES (1.0, 2.0), (2.0, 3.5), (3.0, 7.0), (4.0, 8.5) ) AS v(x, y)
The query returns the following result.
| result |
|---|
|
The following SQL query has only one row where both values are not NULL, so the result is 0.0.
SELECT covar_pop(y, x) AS result FROM (VALUES (1, 2), (3, NULL)) AS v(x, y)
The query returns the following result.
| result |
|---|
|
Differences from Athena engine version 3
In Athena engine version 3, covar_pop of integer columns returns REAL, which keeps about 7 significant digits. In DuckDB, covar_pop returns DOUBLE, so results can differ after the seventh digit.
covar_samp function
Returns the sample covariance of y and x, which is (sum(x * y) - sum(x) * sum(y) / . In this formula, n) / (n - 1) is the number of rows where both values are not nNULL, and each sum covers only those rows. Dividing by one less than corrects for estimating the covariance of a population from a sample. To compute the covariance of those rows as a whole population, use the covar_pop function.n
The function uses only the rows where both y and x are not NULL. If fewer than two rows have both values, the result is NULL.
Syntax
covar_samp(y, x)
Arguments
The covar_samp function takes the following arguments:
y-
The first set of values, as
DOUBLE. Other numeric types are converted toDOUBLE. x-
The second set of values, as
DOUBLE. Other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query returns the sample covariance of four pairs of values. The function divides by 3, one less than the number of pairs, so the result is larger than the population covariance of the same pairs, 2.875.
SELECT covar_samp(y, x) AS result FROM (VALUES (1.0, 2.0), (2.0, 3.5), (3.0, 7.0), (4.0, 8.5) ) AS v(x, y)
The query returns the following result.
| result |
|---|
|
The following SQL query has only one row where both values are not NULL, so the result is NULL.
SELECT covar_samp(y, x) AS result FROM (VALUES (1, 2), (3, NULL)) AS v(x, y)
The query returns the following result.
| result |
|---|
|
Differences from Athena engine version 3
In Athena engine version 3, covar_samp of integer columns returns REAL, which keeps about 7 significant digits. In DuckDB, covar_samp returns DOUBLE, so results can differ after the seventh digit.
entropy function
Counts how often each distinct value of expression occurs, and returns the Shannon entropy of those frequencies, in bits (with base-2 logarithms). The result is 0 when every value is the same, and it grows as the values spread more evenly over more distinct values. For distinct values that occur equally often, the result is nlog2(.n)
NULL values are ignored. If there are no values that are not NULL, the result is 0, not NULL.
Syntax
entropy(expression)
Arguments
The entropy function takes the following argument:
expression-
The values whose entropy the function returns, of any type. The function counts the occurrences of each distinct value.
Return type
DOUBLE
Examples
The following SQL query has four distinct values that each occur once, so the entropy is log2(4).
SELECT entropy(x) AS result FROM (VALUES ('a'), ('b'), ('c'), ('d')) AS v(x)
The query returns the following result.
| result |
|---|
|
The following SQL query has one value that occurs three times and another that occurs once, so the entropy is less than 1.
SELECT entropy(x) AS result FROM (VALUES ('a'), ('a'), ('a'), ('b')) AS v(x)
The query returns the following result.
| result |
|---|
|
The following SQL query has only NULL values, so the result is 0.0, not NULL.
SELECT entropy(x) AS result FROM (VALUES (CAST(NULL AS VARCHAR)), (NULL)) AS v(x)
The query returns the following result.
| result |
|---|
|
The following SQL query treats each value of c as a count of observations, 8 observations in all. It computes the entropy of those counts with sum and log2 instead of entropy. The result is rounded to four decimal places.
SELECT round( log2(sum(c)) - sum(c * log2(c)) / sum(c), 4 ) AS result FROM (VALUES (1), (2), (2), (3)) AS v(c)
The query returns the following result.
| result |
|---|
|
Differences from Athena engine version 3
Athena engine version 3 does not have an entropy function.
kurtosis function
Returns the sample excess kurtosis of the values of expression that are not NULL. The function applies a correction for the number of values, so that the result estimates the excess kurtosis of the larger population that the values come from. Kurtosis measures how heavy the tails of a distribution are. Excess kurtosis is the kurtosis minus 3, so a normal distribution scores 0, a distribution with more extreme outliers scores higher, and a distribution with fewer extreme outliers scores lower.
The function needs at least four values that are not NULL. With fewer values, or when every value is the same, the result is NULL. To compute the kurtosis of the values as a whole population, without the correction, use the kurtosis_pop function.
Syntax
kurtosis(expression)
Arguments
The kurtosis function takes the following argument:
expression-
The values whose kurtosis the function returns, as a
DOUBLEvalue. Other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query returns the sample excess kurtosis of four values.
SELECT kurtosis(x) AS result FROM (VALUES (1), (2), (2), (3)) AS v(x)
The query returns the following result.
| result |
|---|
|
The following SQL query has only three values, so the result is NULL.
SELECT kurtosis(x) AS result FROM (VALUES (1), (2), (3)) AS v(x)
The query returns the following result.
| result |
|---|
|
kurtosis_pop function
Returns the population excess kurtosis of the values of expression that are not NULL: the fourth central moment divided by the square of the variance, minus 3. The function applies no correction for the number of values, so use it when the values are the whole population. For a sample, use the kurtosis function.
With fewer than two values that are not NULL, or when every value is the same, the result is NULL.
Syntax
kurtosis_pop(expression)
Arguments
The kurtosis_pop function takes the following argument:
expression-
The values whose kurtosis the function returns, as a
DOUBLEvalue. Other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query returns the population excess kurtosis of four values.
SELECT kurtosis_pop(x) AS result FROM (VALUES (1), (2), (2), (3)) AS v(x)
The query returns the following result.
| result |
|---|
|
The following SQL query compares the sample and population excess kurtosis of the same five values. With only five values, the correction that kurtosis applies changes the result from -0.5 to 2.0.
SELECT kurtosis(x) AS sample_kurtosis, kurtosis_pop(x) AS population_kurtosis FROM (VALUES (1), (2), (2), (2), (3)) AS v(x)
The query returns the following result.
| sample_kurtosis | population_kurtosis |
|---|---|
|
|
The following SQL query has the same value in every row, so the result is NULL.
SELECT kurtosis_pop(x) AS result FROM (VALUES (2), (2), (2), (2)) AS v(x)
The query returns the following result.
| result |
|---|
|
mad function
Returns the median absolute deviation (MAD) of x: the median of the absolute differences between each value and the median of all the values. Like the standard deviation, it measures how spread out the values are, but a few extreme values change it much less.
NULL values are ignored. If the group has no values that are not NULL, the result is NULL. For date and time values, the result is an INTERVAL.
Syntax
mad(x)
Arguments
The mad function takes the following argument:
x-
The values whose median absolute deviation the function returns, as a numeric value, or a
DATE,TIME,TIME WITH TIME ZONE,TIMESTAMP, orTIMESTAMP WITH TIME ZONEvalue.
Return type
The DECIMAL type of x for DECIMAL input, FLOAT for FLOAT input, DOUBLE for integer and DOUBLE input, and INTERVAL for date and time input.
Examples
The following SQL query returns the median absolute deviation of four values. The median is 2.5, the absolute differences from it are 1.5, 0.5, 0.5, and 1.5, and their median is 1.0.
SELECT mad(x) AS result FROM (VALUES (1.0), (2.0), (3.0), (4.0)) AS v(x)
The query returns the following result.
| result |
|---|
|
The following SQL query compares the median absolute deviation with the population standard deviation for values that include one outlier, 100. The median absolute deviation stays at 1.0, the same as for the values 1 to 4 alone, while the outlier raises the population standard deviation to 39.01.
SELECT mad(x) AS mad_value, round(stddev_pop(x), 2) AS stddev_value FROM (VALUES (1), (2), (3), (4), (100)) AS v(x)
The query returns the following result.
| mad_value | stddev_value |
|---|---|
|
|
The following SQL query returns the median absolute deviation of three dates as an interval.
SELECT mad(d) AS result FROM (VALUES (DATE '2026-10-01'), (DATE '2026-10-03'), (DATE '2026-10-10') ) AS v(d)
The query returns the following result.
| result |
|---|
|
sem function
Returns the standard error of the mean of x. The function computes it as the population standard deviation of the values divided by the square root of the number of values: stddev_pop(x) / sqrt(count(x)).
NULL values are ignored. With one value, the result is 0.0. If every value is NULL, or the group has no rows, the result is NULL.
Syntax
sem(x)
Arguments
The sem function takes the following argument:
x-
The values for which the function computes the standard error of the mean, as
DOUBLEvalues. Values of other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query returns the standard error of the mean of eight values. Their population standard deviation is 2.0, so the result is 2.0 / sqrt(8).
SELECT sem(x) AS result FROM (VALUES (2), (4), (4), (4), (5), (5), (7), (9) ) AS t(x)
The query returns the following result.
| result |
|---|
|
The following SQL query compares sem with the standard error computed from the sample standard deviation.
SELECT sem(x) AS pop_based, stddev_samp(x) / sqrt(count(x)) AS sample_based FROM (VALUES (1.0), (2.0), (3.0), (4.0)) AS t(x)
The query returns the following result.
| pop_based | sample_based |
|---|---|
|
|
The following SQL query has one value, so the result is 0.0, not NULL.
SELECT sem(x) AS result FROM (VALUES (5.0)) AS t(x)
The query returns the following result.
| result |
|---|
|
Considerations
sem uses the population standard deviation, which divides by the number of values. Many statistics tools compute the standard error from the sample standard deviation instead, which divides by one less than the number of values and gives a larger result. To get that result, use stddev_samp(x) / sqrt(count(x)).
skewness function
Returns the sample skewness of x, adjusted for the number of values (the adjusted Fisher-Pearson coefficient of skewness). A positive result means that the values have a longer tail of large values, and a negative result means a longer tail of small values. A result of 0.0 means that the values are spread symmetrically around their mean.
NULL values are ignored. If there are fewer than three values, or if every value is the same, the result is NULL.
Syntax
skewness(x)
Arguments
The skewness function takes the following argument:
x-
The values whose skewness the function returns, as
DOUBLEvalues. Values of other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query has one value that is much larger than the others, so the skewness is positive.
SELECT skewness(x) AS result FROM (VALUES (1), (1), (1), (5)) AS t(x)
The query returns the following result.
| result |
|---|
|
The following SQL query has values spread evenly around their mean, so the skewness is 0.0.
SELECT skewness(x) AS result FROM (VALUES (1), (4), (7)) AS t(x)
The query returns the following result.
| result |
|---|
|
The following SQL query has three equal values, so the result is NULL.
SELECT skewness(x) AS result FROM (VALUES (5), (5), (5)) AS t(x)
The query returns the following result.
| result |
|---|
|
Differences from Athena engine version 3
Athena engine version 3 and DuckDB compute skewness with different formulas, so skewness returns different values for the same input. The difference is largest for small groups.
Athena engine version 3 does not adjust the result for the number of values. For example, for the values 1, 1, 1, and 5, Athena engine version 3 returns about 1.1547, and DuckDB returns 2.0. To get the Athena engine version 3 value, multiply the result by (n - 2) / sqrt(n * (n - 1)), where n is count(x).
When every value is the same, Athena engine version 3 returns NaN, and DuckDB returns NULL.
stddev_pop function
Returns the population standard deviation of x, the square root of var_pop(x). It treats the values as the whole population. For that reason, it divides the sum of the squared differences from the mean by the number of values, and then takes the square root. To estimate the standard deviation of a larger population from a sample, use the stddev_samp function, which divides by one less than the number of values.
NULL values are ignored. With one value, the result is 0.0. If every value is NULL, or the group has no rows, the result is NULL.
Syntax
stddev_pop(x)
Arguments
The stddev_pop function takes the following argument:
x-
The values whose standard deviation the function returns, as
DOUBLEvalues. Values of other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query returns the population standard deviation of eight values.
SELECT stddev_pop(x) AS result FROM (VALUES (2), (4), (4), (4), (5), (5), (7), (9) ) AS t(x)
The query returns the following result.
| result |
|---|
|
The following SQL query has one value. stddev_pop returns 0.0, and stddev_samp returns NULL.
SELECT stddev_pop(x) AS pop, stddev_samp(x) AS samp FROM (VALUES (5.0)) AS t(x)
The query returns the following result.
| pop | samp |
|---|---|
|
|
stddev_samp function
Returns the sample standard deviation of x, the square root of var_samp(x). It treats the values as a sample of a larger population. For that reason, it divides the sum of the squared differences from the mean by one less than the number of values, and then takes the square root. To treat the values as the whole population, use the stddev_pop function.
NULL values are ignored. If there are fewer than two values, the result is NULL.
stddev is an alias of stddev_samp.
Syntax
stddev_samp(x)
Arguments
The stddev_samp function takes the following argument:
x-
The values whose standard deviation the function returns, as
DOUBLEvalues. Values of other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query returns the sample standard deviation of five values. Their sample variance is 10.0, so the result is sqrt(10.0).
SELECT stddev_samp(x) AS result FROM (VALUES (1), (3), (5), (7), (9)) AS t(x)
The query returns the following result.
| result |
|---|
|
The following SQL query uses the stddev alias to return the sample standard deviation of four values.
SELECT stddev(x) AS result FROM (VALUES (1.0), (2.0), (3.0), (4.0)) AS t(x)
The query returns the following result.
| result |
|---|
|
The following SQL query has only one value, so the result is NULL.
SELECT stddev_samp(x) AS result FROM (VALUES (5.0)) AS t(x)
The query returns the following result.
| result |
|---|
|
var_pop function
Returns the population variance of x: the average of the squared differences between each value and the mean of the values. It treats the values as the whole population, so it divides the sum of squared differences by the number of values. To estimate the variance of a larger population from a sample, use the var_samp function, which divides by one less than the number of values.
NULL values are ignored. With one value, the result is 0.0. If every value is NULL, or the group has no rows, the result is NULL.
Syntax
var_pop(x)
Arguments
The var_pop function takes the following argument:
x-
The values whose variance the function returns, as
DOUBLEvalues. Values of other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query returns the population variance of eight values.
SELECT var_pop(x) AS result FROM (VALUES (2), (4), (4), (4), (5), (5), (7), (9) ) AS t(x)
The query returns the following result.
| result |
|---|
|
The following SQL query compares the population variance and the sample variance of the same five values.
SELECT var_pop(x) AS pop, var_samp(x) AS samp FROM (VALUES (1), (2), (3), (4), (5)) AS t(x)
The query returns the following result.
| pop | samp |
|---|---|
|
|
var_samp function
Returns the sample variance of x: the sum of the squared differences between each value and the mean of the values, divided by one less than the number of values. It treats the values as a sample of a larger population. To treat the values as the whole population, use the var_pop function.
NULL values are ignored. If there are fewer than two values, the result is NULL.
variance is an alias of var_samp.
Syntax
var_samp(x)
Arguments
The var_samp function takes the following argument:
x-
The values whose variance the function returns, as
DOUBLEvalues. Values of other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query returns the sample variance of five values.
SELECT var_samp(x) AS result FROM (VALUES (1), (3), (5), (7), (9)) AS t(x)
The query returns the following result.
| result |
|---|
|
The following SQL query uses the variance alias to return the sample variance of four values.
SELECT variance(x) AS result FROM (VALUES (1.0), (2.0), (3.0), (4.0)) AS t(x)
The query returns the following result.
| result |
|---|
|
The following SQL query has only one value, so the result is NULL.
SELECT var_samp(x) AS result FROM (VALUES (5.0)) AS t(x)
The query returns the following result.
| result |
|---|
|