Function Reference

VAR_SAMP

Returns the sample variance, that is, the mean squared deviation from the mean.

MySQL 8.1
VAR_SAMP(expr)

Parameters

exprDOUBLE
Numeric expression

Return value

A floating-point number (DOUBLE); NULLs are ignored; NULL if there are fewer than two values

Examples

Price variance by category

MySQL 8.1
SELECT category,
	ROUND(VAR_SAMP(price), 2) AS price_variance,
	ROUND(STDDEV_SAMP(price), 2) AS price_spread
FROM products
GROUP BY category;
categoryprice_varianceprice_spread
Food40.326.35
Drinks4.432.1
Snacks0.580.76
Grocery82.83

Sample and population variance

MySQL 8.1
SELECT VAR_SAMP(x) AS sample,
	VAR_POP(x) AS population
FROM (
		SELECT 1 AS x
		UNION ALL
		SELECT 2
		UNION ALL
		SELECT 3
		UNION ALL
		SELECT 4
	) AS t;
samplepopulation
1.66666666666666671.25

Details

The sum of squared deviations is divided by the number of values minus 1. The square root of the variance is the standard deviation: SQRT(VAR_SAMP(x)) equals STDDEV_SAMP(x). In PostgreSQL, VARIANCE also calculates the sample variance, while in MySQL VARIANCE is the population one.

See also