PostgreSQL var_samp() Function

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

var_samp() Syntax

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

var_samp(expr)

Typically, we use the var_samp() function like:

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

Parameters

expr

Required. A column name or expression.

Return value

The PostgreSQL var_samp() function returns the sample variance (square of the sample standard deviation) for all non-null input values.

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

var_samp() Examples

To demonstrate usages of the PostgreSQL var_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 var_samp() function to calculate the sample variance of all the values ​​in the x column:

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