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;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;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.