// aggregates reference

SQL STDDEV_SAMP function

Sample standard deviation: divides by n-1 and returns NULL when there are fewer than two non-NULL values.

Categoría
Aggregates
Returns
numeric
In this category
20 of 52

Sintaxis

STDDEV_SAMP(x) | STDEV(x) | STDDEV_SAMP(x) OVER (...)

Soporte del motor

MotorSupportNotas
SQL Server Different name or syntax STDEV is the SQL Server spelling
PostgreSQL Sí Supported under this name.
MySQL Sí Supported under this name.
SQLite Not built in not built in; use an extension or compute SQRT of the manual sample-variance formula
Oracle Sí Supported under this name.

Ejemplos

PostgreSQL

SELECT stddev_samp(price) FROM products WHERE category_id = 7;

SQL Server

SELECT STDEV(price) FROM products WHERE category_id = 7;

MySQL

SELECT STDDEV_SAMP(score) OVER (PARTITION BY exam) FROM results;

Conviene saberlo

  • Fewer than two non-NULL values returns NULL, not 0: a one-row group has no sample deviation.
  • NULLs are excluded before counting, so the denominator is the count of present values minus one.
  • Plain STDDEV is not portable: in PostgreSQL it means STDDEV_SAMP, in MySQL it means STDDEV_POP, and in Oracle it is the sample form except that one row returns 0. SQL Server spells the sample form STDEV.

Questions about STDDEV_SAMP

Sample or population standard deviation?

STDDEV_SAMP divides by n-1 (sample). For the population form dividing by n, use STDDEV_POP.

Why does one row return NULL?

With n = 1 the sample formula divides by zero; the standard says that is undefined, so engines return NULL.

What is STDDEV_SAMP in SQL Server?

STDEV: same computation, shorter name; STDEVP is the population version.

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

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, Oracle.

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