Example 1
Measure the spread of temperature readings from a sensor
The three readings 10, 20, and 30 have a mean of 20. Sample variance divides the total squared deviation from the mean by n − 1 = 2: ((10−20)² + (20−20)² + (30−20)²) / 2 = (100 + 0 + 100) / 2 = 100. Sample standard deviation is √100 = 10. The population variants (STDDEV_POP, VAR_POP, STDEVP, VARP) divide by n = 3 instead, giving variance ≈ 66.67 and standard deviation ≈ 8.16. Use sample variants when working with a subset of a larger population; use population variants when the data represents the complete set.
CREATE TABLE readings (sensor_id INT, temp_c INT);
INSERT INTO
readings
VALUES
(1, 10),
(1, 20),
(1, 30);SELECT
STDDEV_SAMP(temp_c) AS stddev_samp,
VAR_SAMP(temp_c) AS var_samp
FROM
readings;| stddev_samp | var_samp |
|---|---|
| 10 | 100 |
CREATE TABLE readings (sensor_id INT, temp_c INT);
INSERT INTO
readings
VALUES
(1, 10),
(1, 20),
(1, 30);SELECT
STDEV (temp_c) AS stddev_samp,
VAR (temp_c) AS var_samp
FROM
readings;| stddev_samp | var_samp |
|---|---|
| 10 | 100 |
CREATE TABLE readings (sensor_id INT, temp_c INT);
INSERT INTO
readings
VALUES
(1, 10),
(1, 20),
(1, 30);SELECT
SQRT(
(
SUM(temp_c * temp_c) - SUM(temp_c) * SUM(temp_c) * 1.0 / COUNT(*)
) / (COUNT(*) - 1)
) AS stddev_samp,
(
SUM(temp_c * temp_c) - SUM(temp_c) * SUM(temp_c) * 1.0 / COUNT(*)
) / (COUNT(*) - 1) AS var_samp
FROM
readings;| stddev_samp | var_samp |
|---|---|
| 10 | 100 |
MySQL, MariaDB, and PostgreSQL use STDDEV_SAMP/VAR_SAMP. SQL Server uses STDEV/VAR. SQLite requires an arithmetic workaround using SUM and SQRT.