// aggregates reference

SQL VARIANCE function

The variance aggregate whose definition silently changes between engines.

카테고리
Aggregates
Returns
numeric
In this category
22 of 52

구문

VARIANCE(x) | VAR_SAMP(x) | VAR_POP(x)

엔진 지원

엔진Support메모
SQL Server Different name or syntax SQL Server spells it VAR (sample) and VARP (population); no VARIANCE
PostgreSQL 예 VARIANCE is a historical alias of var_samp
MySQL 예 MySQL's VARIANCE is population variance, an alias of VAR_POP
SQLite Not built in No built-in equivalent.
Oracle 예 Oracle's VARIANCE returns 0 instead of NULL for a single row

예시

PostgreSQL

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

MySQL

SELECT VARIANCE(score) FROM results WHERE exam = 'A';

Oracle

SELECT VARIANCE(salary) FROM employees;

알아 두면 좋은 정보

  • The same query returns different numbers per engine: sample variance in PostgreSQL, population variance in MySQL.
  • Oracle's single-row behaviour is the odd one out: VARIANCE gives 0 where VAR_SAMP gives NULL.
  • NULLs are skipped everywhere; an empty group returns NULL.
  • Prefer the explicit VAR_SAMP / VAR_POP spellings so the intent survives a port.

Questions about VARIANCE

Is VARIANCE sample or population?

Depends on the engine: PostgreSQL sample, MySQL population, Oracle sample except it returns 0 for one row. Use VAR_SAMP or VAR_POP to be explicit.

How does variance relate to standard deviation?

Standard deviation is the square root of variance; MySQL's STDDEV uses its population VARIANCE.

What is VARIANCE in SQL Server?

VAR (sample) or VARP (population); there is no VARIANCE spelling.

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.