STDDEV_SAMP
Returns the sample standard deviation, that is, how far the values deviate from the mean on average.
MySQL 8.1
STDDEV_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 spread by category
MySQL 8.1
SELECT category,
ROUND(AVG(price), 2) AS avg_price,
ROUND(STDDEV_SAMP(price), 2) AS price_spread
FROM products
GROUP BY category;Sample and population deviation
MySQL 8.1
SELECT STDDEV_SAMP(x) AS sample,
STDDEV_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 sample deviation divides the sum of squared deviations by the number of values minus 1 and fits when the rows are a sample from a larger set. For a single value the result is NULL. The population deviation (division by the number of values) is calculated by STDDEV_POP. In PostgreSQL, STDDEV is a synonym for this function, while in MySQL it is a synonym for STDDEV_POP.