// aggregates reference

SQL STDDEV function

Measures how spread out a group's values are around their average. Engines disagree on the name and on whether the plain STDDEV means sample or population, and SQLite has no built-in.

Categoría
Aggregates
Returns
numeric / double
In this category
9 of 52

Sintaxis

STDEV(expr)  -- SQL Server
STDDEV_SAMP(expr) / STDDEV_POP(expr)  -- others

Each block is a separate form. The support table and the examples show which engine takes which.

Soporte del motor

MotorSupportNotas
SQL Server Different name or syntax spelled STDEV and STDEVP
PostgreSQL Sí Supported under this name.
MySQL Sí STDDEV and STD are the population version; STDDEV_SAMP is the sample one
SQLite Not built in not built in; compute it from SUM/COUNT or add an extension
Oracle Sí STDDEV_SAMP, STDDEV_POP and STDDEV all available

Ejemplos

SQL Server

SELECT STDEV(response_ms) AS sample_spread
FROM pings;

PostgreSQL

SELECT STDDEV_SAMP(response_ms) AS sample_spread
FROM pings;

MySQL

-- plain STDDEV would be the population version here
SELECT STDDEV_SAMP(response_ms) AS sample_spread
FROM pings;

Conviene saberlo

  • Sample (STDDEV_SAMP / STDEV) divides by n-1, population (STDDEV_POP / STDEVP) by n. Pick sample when your rows are a sample of a larger population.
  • The plain name is not consistent: STDDEV is the sample version on PostgreSQL and Oracle but the population version on MySQL. Values 1 and 3 give 1.414 on PostgreSQL and 1 on MySQL. Spell out STDDEV_SAMP or STDDEV_POP.
  • One row: STDDEV_SAMP returns NULL and STDDEV_POP returns 0. Oracle's plain STDDEV returns 0 for one row even though it is otherwise the sample version.

Questions about STDDEV / STDEV

What is the difference between STDDEV_SAMP and STDDEV_POP?

The divisor: n-1 for the sample version, n for the population version. Use the sample one unless your rows really are the entire population.

Does SQLite have STDDEV?

No, SQLite ships no aggregate for it. The sample version is SQRT((SUM(x*x) - SUM(x)*SUM(x)/COUNT(x)) / (COUNT(x) - 1)), using the SQRT from SQLite's math functions (3.35+), or load an extension.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.