PostgreSQL stddev_samp() Function

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

stddev_samp() Syntax

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

stddev_samp(expr)

Typically, we use the stddev_samp() function like:

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

Parameters

expr

Required. A column name or expression.

Return value

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

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

stddev_samp() Examples

To demonstrate usages of the PostgreSQL stddev_samp() 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_samp() function to calculate the sample standard deviation of all the values ​​in the x column:

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