Back to Library

STDEV() in SQL

Calculates the standard deviation (population or sample) of a set of numeric values.

Syntax

Population Standard Deviation: STDDEV_POP(expression) or STDEVP(expression)
Sample Standard Deviation: STDDEV_SAMP(expression) or STDEV(expression)
Return type
FLOAT/DOUBLE/DECIMAL

STDEV() Function Example

SELECT STDEV(salary) FROM employees; 
-- Output: 15652.47 (standard deviation of salaries)

What is STDEV() in SQL?

The STDEV() function in SQL calculates the standard deviation of a set of numeric values, measuring how much the values deviate from the mean (average). It is commonly used in statistical analysis, financial modeling, and quality control to assess variability in data.

SQL databases typically provide two types of standard deviation calculations:

  • Population Standard Deviation (STDDEV_POP()) → Measures standard deviation for an entire dataset.

  • Sample Standard Deviation (STDDEV_SAMP()) → Measures standard deviation for a sample from a dataset.

This function is supported in SQL Server, PostgreSQL, MySQL (as STDDEV_POP() and STDDEV_SAMP()), and Oracle.

Parameters:

  • expression: Numeric column or expression to calculate standard deviation

Example Use Cases:

-- Calculate salary spread by department

SELECT department, STDEV(salary) AS salary_spread FROM employees GROUP BY department; 
-- Output:
department    salary_spread
-----------   -------------
Sales         12450.75
Engineering   15678.23
Marketing      9876.44
HR             7234.89
Finance       11567.32

-- Measure price variation 

SELECT category, STDDEV_POP(price) AS price_deviation FROM products GROUP BY category; 
-- Output:
category        price_deviation
--------------  ---------------
Electronics          245.67
Clothing             34.89
Home & Garden        89.45
Books                12.34
Sports Equipment    156.78

-- Analyze score distribution 

SELECT course_id, STDDEV_SAMP(score) AS score_spread FROM student_scores GROUP BY course_id;
-- Output:
course_id    score_spread
---------    ------------
CS101        8.45
MATH201      12.67
ENG102       6.89
PHYS301      14.23
CHEM201      10.56

Notes:

  • Behavior: Ignores NULL values in calculation

  • Performance Considerations: Square root of variance calculation

  • Version Info: Available in most major DBMS with varying syntax

  • 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

STD(), STDDEV(), STDDEV_POP(), STDDEV_SAMP()

STD(salary)

Ignores NULL

PostgreSQL

STDDEV(), STDDEV_POP(), STDDEV_SAMP()

STDDEV(salary)

Ignores NULL

SQL Server

STDEV(), STDEVP()

STDEV(salary)

Ignores NULL

SQLite

STDEV()

STDEV(salary)

Ignores NULL

BigQuery

STDDEV(), STDDEV_POP(), STDDEV_SAMP()

STDDEV(salary)

Ignores NULL

Snowflake

STDDEV(), STDDEV_POP(), STDDEV_SAMP()

STDDEV(salary)

Ignores NULL

Athena

STDDEV(), STDDEV_POP(), STDDEV_SAMP()

STDDEV(salary)

Ignores NULL

Related Functions:

STDEV()

Calculates the standard deviation (population or sample) of a set of numeric values.

Calculates the standard deviation (population or sample) of a set of numeric values.

Calculates the standard deviation (population or sample) of a set of numeric values.

Calculates the standard deviation (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