// aggregates reference

SQL VAR_SAMP function

Sample variance: divides by n-1 and needs at least two non-NULL values.

类别
Aggregates
Returns
numeric
In this category
23 of 52

语法

VAR_SAMP(x) | VAR(x)

引擎支持

引擎Support备注
SQL Server Different name or syntax VAR is the SQL Server spelling
PostgreSQL 是 Supported under this name.
MySQL 是 Supported under this name.
SQLite Not built in not built in; use an extension or SUM((x-avg)*(x-avg))/(COUNT(x)-1)
Oracle 是 Supported under this name.

示例

PostgreSQL

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

SQL Server

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

值得了解

  • One non-NULL value returns NULL; two or more divide by count-1.
  • NULLs are skipped, so the denominator counts only present values.
  • In SQLite the whole family is missing; an extension or a manual SUM((x-avg)*(x-avg))/(COUNT(x)-1) formula fills the gap.

Questions about VAR_SAMP

VAR_SAMP vs VAR_POP?

The divisor: n-1 versus n, and the one-row result: NULL versus 0.

What is VAR_SAMP in SQL Server?

VAR.

How do I get standard deviation from it?

Take the square root, or call STDDEV_SAMP directly.

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.