PostgreSQL stddev() Function

The PostgreSQL stddev() function is an aggregate function that computes the sample standard deviation of all non-null input values. It is an alias of stddev_samp().

stddev() Syntax

Here is the syntax of the PostgreSQL stddev() function:

stddev(expr)

Typically, we use the stddev() function like:

SELECT stddev(expr), ...
FROM table_name
[WHERE ...]
[GROUP BY group_expr1, group_expr2, ...];

Parameters

expr

Required. A column name or expression.

Return value

The PostgreSQL stddev() function returns the sample standard deviation of all non-null input values.

Note that the stddev() function only handles non-null values. That is, null values ​​are ignored by the stddev() function.

stddev() Examples

To demonstrate usages of the PostgreSQL stddev() function, we simulate a temporary table using the following statement with UNION and SELECT:

SELECT 4 x
UNION
SELECT 5 x
UNION
SELECT 6 x;
 x
---
 4
 6
 5
(3 rows)

The following statement uses the stddev() function to calculate the sample standard deviation of all the values ​​in the x column:

SELECT stddev(x)
FROM (
    SELECT 4 x
    UNION
    SELECT 5 x
    UNION
    SELECT 6 x
  ) t;
         stddev
------------------------
 1.00000000000000000000
(1 rows)