VARIANCE() in SQL
Calculates the statistical variance (population or sample) of a set of numeric values.
Syntax
Return type
VARIANCE() Function Example
What is VARIANCE() in SQL?
The VARIANCE()
function in SQL calculates the statistical variance of a set of numeric values, measuring how much the values deviate from the mean (average). It is useful in data analysis, financial modeling, and performance evaluation to understand the spread of data points.
SQL databases typically offer two types of variance calculations:
Population Variance (
VAR_POP()
) → Measures variance across an entire dataset.Sample Variance (
VAR_SAMP()
) → Measures variance for a sample from a dataset.
This function is supported in SQL Server, PostgreSQL, MySQL (as VAR_POP()
and VAR_SAMP()
), and Oracle.
Parameters:
expression: Numeric column or expression to calculate variance
Example Use Cases:
-- Calculate salary variance by department
-- Find price variation among products
-- Measure score distribution
Notes:
Behavior: Ignores NULL values in calculation
Performance Considerations: Computationally intensive
Version Info: Available in most major DBMS
Deprecated/Recommended Alternatives: None
Error Handling:
Error: Returns NULL if insufficient non-NULL values
Recommendation: Use COALESCE for NULL handling
Supported Databases:
DBMS
Function / Syntax
Example
Behavior with NULL
MySQL
VARIANCE(), VAR_POP(), VAR_SAMP()
VARIANCE(salary)
Ignores NULL
PostgreSQL
VAR_POP(), VAR_SAMP()
VAR_POP(salary)
Ignores NULL
SQL Server
VAR(), VARP()
VAR(salary)
Ignores NULL
SQLite
VARIANCE()
VARIANCE(salary)
Ignores NULL
BigQuery
VARIANCE(), VAR_POP(), VAR_SAMP()
VARIANCE(salary)
Ignores NULL
Snowflake
VARIANCE(), VAR_POP(), VAR_SAMP()
VARIANCE(salary)
Ignores NULL
Athena
VAR_POP(), VAR_SAMP()
VAR_POP(salary)
Ignores NULL