SQL VARIANCE function
The variance aggregate whose definition silently changes between engines.
- Category
- Aggregates
- Returns
numeric- In this category
- 22 of 52
Syntax
VARIANCE(x) | VAR_SAMP(x) | VAR_POP(x)
Engine support
| Engine | Support | Notes |
|---|---|---|
| SQL Server | Different name or syntax | SQL Server spells it VAR (sample) and VARP (population); no VARIANCE |
| PostgreSQL | Yes | VARIANCE is a historical alias of var_samp |
| MySQL | Yes | MySQL's VARIANCE is population variance, an alias of VAR_POP |
| SQLite | Not built in | No built-in equivalent. |
| Oracle | Yes | Oracle's VARIANCE returns 0 instead of NULL for a single row |
Examples
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;
Good to know
- 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.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, Oracle.
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.