Function Reference

VAR_SAMP

Returns the sample variance of the expression values in a group of rows.

PostgreSQL 17.5
VAR_SAMP(expression)

Parameters

expressionnumeric | double precision
Numeric expression

Return value

A number: numeric for integers and numeric, double precision for floating-point numbers; NULLs are ignored; NULL if there are fewer than two values

Examples

Sample and population variance

PostgreSQL 17.5
SELECT VAR_SAMP(x) AS sample_variance,
	VAR_POP(x) AS population_variance
FROM (
		VALUES (2),
			(4),
			(4),
			(4),
			(5),
			(5),
			(7),
			(9)
	) AS t(x);
sample_variancepopulation_variance
4.57142857142857144.0000000000000000

Price spread for family members

PostgreSQL 17.5
SELECT family_member,
	COUNT(*) AS payments,
	ROUND(VAR_SAMP(unit_price), 1) AS price_variance
FROM Payments
GROUP BY family_member
ORDER BY family_member;
family_memberpaymentsprice_variance
1812150839.4
27601306939.6
35899303.2
4220000.0
569618.7

Details

The sample variance divides the sum of squared deviations from the mean by n − 1, and the population variance VAR_POP by n. VARIANCE is a synonym for VAR_SAMP.

See also