

# 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
<a name="athena-duckdb-functions-aggregate-corr"></a>

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
<a name="athena-duckdb-functions-aggregate-corr-syntax"></a>

```sql
corr({{y}}, {{x}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-corr-arguments"></a>

The `corr` function takes the following arguments:

**{{y}}**  
The dependent values, as `DOUBLE`. Other numeric types are converted to `DOUBLE`.

**{{x}}**  
The independent values, as `DOUBLE`. Other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-aggregate-corr-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-aggregate-corr-examples"></a>

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`.

```sql
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 | 
| --- | 
| `0.9852117548196743` | 

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`.

```sql
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 | 
| --- | 
| `1.0` | 

The following SQL query shows that the result is `NaN` when `y` has the same value in every row.

```sql
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 | 
| --- | 
| `true` | 

### Considerations
<a name="athena-duckdb-functions-aggregate-corr-considerations"></a>

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
<a name="athena-duckdb-functions-aggregate-corr-differences-from-engine-version-3"></a>

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
<a name="athena-duckdb-functions-aggregate-covar-pop"></a>

Returns the population covariance of `y` and `x`, which is `(sum(x * y) - sum(x) * sum(y) / {{n}}) / {{n}}`. In this formula, `{{n}}` is the number of rows where both values are not `NULL`, 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](#athena-duckdb-functions-aggregate-covar-samp).

### Syntax
<a name="athena-duckdb-functions-aggregate-covar-pop-syntax"></a>

```sql
covar_pop({{y}}, {{x}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-covar-pop-arguments"></a>

The `covar_pop` function takes the following arguments:

**{{y}}**  
The first set of values, as `DOUBLE`. Other numeric types are converted to `DOUBLE`.

**{{x}}**  
The second set of values, as `DOUBLE`. Other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-aggregate-covar-pop-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-aggregate-covar-pop-examples"></a>

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.

```sql
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 | 
| --- | 
| `2.875` | 

The following SQL query has only one row where both values are not `NULL`, so the result is `0.0`.

```sql
SELECT covar_pop(y, x) AS result
FROM (VALUES (1, 2), (3, NULL)) AS v(x, y)
```

The query returns the following result.


| result | 
| --- | 
| `0.0` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-aggregate-covar-pop-differences-from-engine-version-3"></a>

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
<a name="athena-duckdb-functions-aggregate-covar-samp"></a>

Returns the sample covariance of `y` and `x`, which is `(sum(x * y) - sum(x) * sum(y) / {{n}}) / ({{n}} - 1)`. In this formula, `{{n}}` is the number of rows where both values are not `NULL`, and each sum covers only those rows. Dividing by one less than `{{n}}` 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](#athena-duckdb-functions-aggregate-covar-pop).

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
<a name="athena-duckdb-functions-aggregate-covar-samp-syntax"></a>

```sql
covar_samp({{y}}, {{x}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-covar-samp-arguments"></a>

The `covar_samp` function takes the following arguments:

**{{y}}**  
The first set of values, as `DOUBLE`. Other numeric types are converted to `DOUBLE`.

**{{x}}**  
The second set of values, as `DOUBLE`. Other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-aggregate-covar-samp-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-aggregate-covar-samp-examples"></a>

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`.

```sql
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 | 
| --- | 
| `3.8333333333333335` | 

The following SQL query has only one row where both values are not `NULL`, so the result is `NULL`.

```sql
SELECT covar_samp(y, x) AS result
FROM (VALUES (1, 2), (3, NULL)) AS v(x, y)
```

The query returns the following result.


| result | 
| --- | 
| `NULL` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-aggregate-covar-samp-differences-from-engine-version-3"></a>

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
<a name="athena-duckdb-functions-aggregate-entropy"></a>

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 `{{n}}` distinct values that occur equally often, the result is `log2({{n}})`.

`NULL` values are ignored. If there are no values that are not `NULL`, the result is `0`, not `NULL`.

### Syntax
<a name="athena-duckdb-functions-aggregate-entropy-syntax"></a>

```sql
entropy({{expression}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-entropy-arguments"></a>

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
<a name="athena-duckdb-functions-aggregate-entropy-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-aggregate-entropy-examples"></a>

The following SQL query has four distinct values that each occur once, so the entropy is `log2(4)`.

```sql
SELECT entropy(x) AS result
FROM (VALUES ('a'), ('b'), ('c'), ('d')) AS v(x)
```

The query returns the following result.


| result | 
| --- | 
| `2.0` | 

The following SQL query has one value that occurs three times and another that occurs once, so the entropy is less than `1`.

```sql
SELECT entropy(x) AS result
FROM (VALUES ('a'), ('a'), ('a'), ('b')) AS v(x)
```

The query returns the following result.


| result | 
| --- | 
| `0.8112781244591329` | 

The following SQL query has only `NULL` values, so the result is `0.0`, not `NULL`.

```sql
SELECT entropy(x) AS result
FROM (VALUES (CAST(NULL AS VARCHAR)), (NULL)) AS v(x)
```

The query returns the following result.


| result | 
| --- | 
| `0.0` | 

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.

```sql
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 | 
| --- | 
| `1.9056` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-aggregate-entropy-differences-from-engine-version-3"></a>

Athena engine version 3 does not have an `entropy` function.

## `kurtosis` function
<a name="athena-duckdb-functions-aggregate-kurtosis"></a>

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](#athena-duckdb-functions-aggregate-kurtosis-pop).

### Syntax
<a name="athena-duckdb-functions-aggregate-kurtosis-syntax"></a>

```sql
kurtosis({{expression}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-kurtosis-arguments"></a>

The `kurtosis` function takes the following argument:

**{{expression}}**  
The values whose kurtosis the function returns, as a `DOUBLE` value. Other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-aggregate-kurtosis-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-aggregate-kurtosis-examples"></a>

The following SQL query returns the sample excess kurtosis of four values.

```sql
SELECT kurtosis(x) AS result
FROM (VALUES (1), (2), (2), (3)) AS v(x)
```

The query returns the following result.


| result | 
| --- | 
| `1.5` | 

The following SQL query has only three values, so the result is `NULL`.

```sql
SELECT kurtosis(x) AS result
FROM (VALUES (1), (2), (3)) AS v(x)
```

The query returns the following result.


| result | 
| --- | 
| `NULL` | 

## `kurtosis_pop` function
<a name="athena-duckdb-functions-aggregate-kurtosis-pop"></a>

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](#athena-duckdb-functions-aggregate-kurtosis).

With fewer than two values that are not `NULL`, or when every value is the same, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-aggregate-kurtosis-pop-syntax"></a>

```sql
kurtosis_pop({{expression}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-kurtosis-pop-arguments"></a>

The `kurtosis_pop` function takes the following argument:

**{{expression}}**  
The values whose kurtosis the function returns, as a `DOUBLE` value. Other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-aggregate-kurtosis-pop-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-aggregate-kurtosis-pop-examples"></a>

The following SQL query returns the population excess kurtosis of four values.

```sql
SELECT kurtosis_pop(x) AS result
FROM (VALUES (1), (2), (2), (3)) AS v(x)
```

The query returns the following result.


| result | 
| --- | 
| `-1.0` | 

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`.

```sql
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 | 
| --- | --- | 
| `2.0` | `-0.5` | 

The following SQL query has the same value in every row, so the result is `NULL`.

```sql
SELECT kurtosis_pop(x) AS result
FROM (VALUES (2), (2), (2), (2)) AS v(x)
```

The query returns the following result.


| result | 
| --- | 
| `NULL` | 

## `mad` function
<a name="athena-duckdb-functions-aggregate-mad"></a>

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
<a name="athena-duckdb-functions-aggregate-mad-syntax"></a>

```sql
mad({{x}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-mad-arguments"></a>

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`, or `TIMESTAMP WITH TIME ZONE` value.

### Return type
<a name="athena-duckdb-functions-aggregate-mad-return-type"></a>

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
<a name="athena-duckdb-functions-aggregate-mad-examples"></a>

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`.

```sql
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 | 
| --- | 
| `1.0` | 

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`.

```sql
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 | 
| --- | --- | 
| `1.0` | `39.01` | 

The following SQL query returns the median absolute deviation of three dates as an interval.

```sql
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 | 
| --- | 
| `2 days` | 

## `sem` function
<a name="athena-duckdb-functions-aggregate-sem"></a>

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
<a name="athena-duckdb-functions-aggregate-sem-syntax"></a>

```sql
sem({{x}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-sem-arguments"></a>

The `sem` function takes the following argument:

**{{x}}**  
The values for which the function computes the standard error of the mean, as `DOUBLE` values. Values of other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-aggregate-sem-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-aggregate-sem-examples"></a>

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)`.

```sql
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 | 
| --- | 
| `0.7071067811865475` | 

The following SQL query compares `sem` with the standard error computed from the sample standard deviation.

```sql
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 | 
| --- | --- | 
| `0.5590169943749475` | `0.6454972243679028` | 

The following SQL query has one value, so the result is `0.0`, not `NULL`.

```sql
SELECT sem(x) AS result FROM (VALUES (5.0)) AS t(x)
```

The query returns the following result.


| result | 
| --- | 
| `0.0` | 

### Considerations
<a name="athena-duckdb-functions-aggregate-sem-considerations"></a>

`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
<a name="athena-duckdb-functions-aggregate-skewness"></a>

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
<a name="athena-duckdb-functions-aggregate-skewness-syntax"></a>

```sql
skewness({{x}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-skewness-arguments"></a>

The `skewness` function takes the following argument:

**{{x}}**  
The values whose skewness the function returns, as `DOUBLE` values. Values of other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-aggregate-skewness-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-aggregate-skewness-examples"></a>

The following SQL query has one value that is much larger than the others, so the skewness is positive.

```sql
SELECT skewness(x) AS result
FROM (VALUES (1), (1), (1), (5)) AS t(x)
```

The query returns the following result.


| result | 
| --- | 
| `2.0` | 

The following SQL query has values spread evenly around their mean, so the skewness is `0.0`.

```sql
SELECT skewness(x) AS result
FROM (VALUES (1), (4), (7)) AS t(x)
```

The query returns the following result.


| result | 
| --- | 
| `0.0` | 

The following SQL query has three equal values, so the result is `NULL`.

```sql
SELECT skewness(x) AS result
FROM (VALUES (5), (5), (5)) AS t(x)
```

The query returns the following result.


| result | 
| --- | 
| `NULL` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-aggregate-skewness-differences-from-engine-version-3"></a>

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
<a name="athena-duckdb-functions-aggregate-stddev-pop"></a>

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](#athena-duckdb-functions-aggregate-stddev-samp), 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
<a name="athena-duckdb-functions-aggregate-stddev-pop-syntax"></a>

```sql
stddev_pop({{x}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-stddev-pop-arguments"></a>

The `stddev_pop` function takes the following argument:

**{{x}}**  
The values whose standard deviation the function returns, as `DOUBLE` values. Values of other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-aggregate-stddev-pop-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-aggregate-stddev-pop-examples"></a>

The following SQL query returns the population standard deviation of eight values.

```sql
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 | 
| --- | 
| `2.0` | 

The following SQL query has one value. `stddev_pop` returns `0.0`, and `stddev_samp` returns `NULL`.

```sql
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 | 
| --- | --- | 
| `0.0` | `NULL` | 

## `stddev_samp` function
<a name="athena-duckdb-functions-aggregate-stddev-samp"></a>

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](#athena-duckdb-functions-aggregate-stddev-pop).

`NULL` values are ignored. If there are fewer than two values, the result is `NULL`.

`stddev` is an alias of `stddev_samp`.

### Syntax
<a name="athena-duckdb-functions-aggregate-stddev-samp-syntax"></a>

```sql
stddev_samp({{x}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-stddev-samp-arguments"></a>

The `stddev_samp` function takes the following argument:

**{{x}}**  
The values whose standard deviation the function returns, as `DOUBLE` values. Values of other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-aggregate-stddev-samp-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-aggregate-stddev-samp-examples"></a>

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)`.

```sql
SELECT stddev_samp(x) AS result
FROM (VALUES (1), (3), (5), (7), (9)) AS t(x)
```

The query returns the following result.


| result | 
| --- | 
| `3.1622776601683795` | 

The following SQL query uses the `stddev` alias to return the sample standard deviation of four values.

```sql
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 | 
| --- | 
| `1.2909944487358056` | 

The following SQL query has only one value, so the result is `NULL`.

```sql
SELECT stddev_samp(x) AS result
FROM (VALUES (5.0)) AS t(x)
```

The query returns the following result.


| result | 
| --- | 
| `NULL` | 

## `var_pop` function
<a name="athena-duckdb-functions-aggregate-var-pop"></a>

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](#athena-duckdb-functions-aggregate-var-samp), 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
<a name="athena-duckdb-functions-aggregate-var-pop-syntax"></a>

```sql
var_pop({{x}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-var-pop-arguments"></a>

The `var_pop` function takes the following argument:

**{{x}}**  
The values whose variance the function returns, as `DOUBLE` values. Values of other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-aggregate-var-pop-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-aggregate-var-pop-examples"></a>

The following SQL query returns the population variance of eight values.

```sql
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 | 
| --- | 
| `4.0` | 

The following SQL query compares the population variance and the sample variance of the same five values.

```sql
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 | 
| --- | --- | 
| `2.0` | `2.5` | 

## `var_samp` function
<a name="athena-duckdb-functions-aggregate-var-samp"></a>

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](#athena-duckdb-functions-aggregate-var-pop).

`NULL` values are ignored. If there are fewer than two values, the result is `NULL`.

`variance` is an alias of `var_samp`.

### Syntax
<a name="athena-duckdb-functions-aggregate-var-samp-syntax"></a>

```sql
var_samp({{x}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-var-samp-arguments"></a>

The `var_samp` function takes the following argument:

**{{x}}**  
The values whose variance the function returns, as `DOUBLE` values. Values of other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-aggregate-var-samp-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-aggregate-var-samp-examples"></a>

The following SQL query returns the sample variance of five values.

```sql
SELECT var_samp(x) AS result
FROM (VALUES (1), (3), (5), (7), (9)) AS t(x)
```

The query returns the following result.


| result | 
| --- | 
| `10.0` | 

The following SQL query uses the `variance` alias to return the sample variance of four values.

```sql
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 | 
| --- | 
| `1.6666666666666667` | 

The following SQL query has only one value, so the result is `NULL`.

```sql
SELECT var_samp(x) AS result FROM (VALUES (5.0)) AS t(x)
```

The query returns the following result.


| result | 
| --- | 
| `NULL` | 