Back to Library

VARIANCE() in SQL

Calculates the statistical variance (population or sample) of a set of numeric values.

Syntax

Population Variance: VAR_POP(expression)
Sample Variance: VAR_SAMP(expression) or VARIANCE(expression)
Return type
FLOAT/DOUBLE/DECIMAL

VARIANCE() Function Example

SELECT VARIANCE(salary) FROM employees; 
-- Output: 245000000 (variance of salaries)

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

SELECT department, VARIANCE(salary) AS salary_variance FROM employees GROUP BY department; 
-- Output:
department     salary_variance
------------   ---------------
Engineering     15625000.00
Marketing        9216000.00
Sales           12250000.00
HR               4840000.00
IT              11560000.00

Note: Higher variance indicates more salary spread

-- Find price variation among products 

SELECT category, VAR_POP(price) AS price_variance FROM products GROUP BY category; 
-- Output:

category        price_variance
-------------   --------------
Electronics      125400.25
Clothing          5625.75
Books             1225.50
Accessories       2304.80
Home & Garden     8100.60
Note: Population variance of all prices in category

-- Measure score distribution 

SELECT course_id, VAR_SAMP(score) AS score_variance FROM student_scores GROUP BY course_id;
-- Output:
course_id    course_name        score_variance
----------   -------------      --------------
CS101        Programming 101        64.50
MATH201      Statistics            89.75
ENG102       English Comp          45.25
PHYS101      Physics I            102.80
CHEM101      Chemistry            78.90

Note: Sample variance of student scores

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

Related Functions:

VARIANCE()

Calculates the statistical variance (population or sample) of a set of numeric values.

Calculates the statistical variance (population or sample) of a set of numeric values.

Calculates the statistical variance (population or sample) of a set of numeric values.

Calculates the statistical variance (population or sample) of a set of numeric values.

STRING_AGG()

Concatenates values from multiple rows into a single string, with specified delimiter.

Concatenates values from multiple rows into a single string, with specified delimiter.

Concatenates values from multiple rows into a single string, with specified delimiter.

Concatenates values from multiple rows into a single string, with specified delimiter.

GROUP_CONCAT()

Concatenates values from multiple rows into a single string, with optional delimiter and ordering.

Concatenates values from multiple rows into a single string, with optional delimiter and ordering.

Concatenates values from multiple rows into a single string, with optional delimiter and ordering.

Concatenates values from multiple rows into a single string, with optional delimiter and ordering.

MAX()

Returns the largest value from a specified column or expression, ignoring NULL values.

Returns the largest value from a specified column or expression, ignoring NULL values.

Returns the largest value from a specified column or expression, ignoring NULL values.

Returns the largest value from a specified column or expression, ignoring NULL values.

MIN()

Returns the smallest value from a specified column or expression, ignoring NULL values.

Returns the smallest value from a specified column or expression, ignoring NULL values.

Returns the smallest value from a specified column or expression, ignoring NULL values.

Returns the smallest value from a specified column or expression, ignoring NULL values.

SUM()

Calculates the total sum of all values in a specified column or expression, ignoring NULL values.

Calculates the total sum of all values in a specified column or expression, ignoring NULL values.

Calculates the total sum of all values in a specified column or expression, ignoring NULL values.

Calculates the total sum of all values in a specified column or expression, ignoring NULL values.

COUNT()

Counts the number of rows or non-null values in a specified column. COUNT(*) counts all rows including nulls, while COUNT(column) counts non-null values in the specified column.

Counts the number of rows or non-null values in a specified column. COUNT(*) counts all rows including nulls, while COUNT(column) counts non-null values in the specified column.

Counts the number of rows or non-null values in a specified column. COUNT(*) counts all rows including nulls, while COUNT(column) counts non-null values in the specified column.

Counts the number of rows or non-null values in a specified column. COUNT(*) counts all rows including nulls, while COUNT(column) counts non-null values in the specified column.

One place for all your queries,
directly on your SQL editor