Computes the sample variance of all non-NULL numeric values produced by an expression. The sample variance measures the average of the squared differences from the sample mean, indicating how spread out the values are within a sample.
For information about the population variance, which measures how spread out the values are within the full population, see VAR_POP.
Alias: VARIANCE
Syntax
{ VARIANCE | VAR_SAMP }(<expression>) [FILTER ([WHERE] <condition>)]
Parameters
| Parameter | Description | Supported input types |
|---|
<expression> | An expression producing numeric values for which to calculate the sample variance. | REAL, DOUBLE PRECISION |
<condition> | An optional boolean expression to filter rows used in aggregation | BOOL |
Return Type
VAR_SAMP returns a result of type DOUBLE PRECISION.
Special cases
- If there is at most one non-
NULL input value, the result is NULL.
- If the input contains an
Inf or NaN value, the result will be NaN.
Example
The following example calculates the sample variance of five grade values:
SELECT ROUND(VAR_SAMP(grade::double precision), 3) AS variance
FROM UNNEST(ARRAY[4.0, 3.7, 3.3, 2.7, 2.7]) AS t(grade);
| variance double null |
|---|
| 0.342 |
Rows: 1Execution time: 5.95ms