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

カテゴリ
Aggregates
Returns
numeric / double
In this category
9 of 52

構文

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.

エンジンサポート

エンジンSupportメモ
SQL Server Different name or syntax spelled STDEV and STDEVP
PostgreSQL はい Supported under this name.
MySQL はい 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 はい STDDEV_SAMP, STDDEV_POP and STDDEV all available

例

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;

知っておくと便利なこと

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