MySQL
ORACLE
POSTGRESQL
SQL
SQL SERVER

A Guide to the SQL Standard Deviation Functions

intro

Let’s learn how to use SQL standard deviation functions to calculate the standard deviation of numeric columns in your database.

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT

Standard deviation is a measure of the amount of variation or dispersion in a list of values. It indicates how spread out the numbers are in a dataset, with a higher standard deviation showing greater variability. This operation is widely used in statistical analysis and is so common that SQL provides functions to compute it. Discover the SQL standard deviation functions and learn how to apply them to your data!

What Are the SQL Standard Deviation Functions?

Standard SQL provides two functions for calculating standard deviation:

  • STDDEV_SAMP(): To calculate the sample standard deviation.
  • STDDEV_POP(): To calculate the population standard deviation.

See the difference between sample and population standard deviation.

The STDDEV_POP() and STDDEV_SAMP() functions are supported by databases like MySQL, PostgreSQL, and Oracle. SQL Server uses STDEV() for sample standard deviation and STDEVP() for population standard deviation instead.

Some databases also offer STDDEV(), but its meaning varies. For example:

  • In MySQL, STDDEV() is a synonym for STDDEV_POP().
  • In PostgreSQL, STDDEV() is a synonym for STDDEV_SAMP().
  • In Oracle, STTDEV() differs from STDDEV_SAMP() because it returns 0 when applied to record sets with only 1 row of input data, whereas STDDEV_SAMP() returns NULL.

Note: All the above functions are aggregate functions, which means they operate on multiple rows and return a single result. You can only use them in SELECT, HAVING, ORDER BY clauses, or with GROUP BY when applied to grouped data.

Summary of Standard Deviation SQL Functions

DatabaseSample Standard DeviationPopulation Standard DeviationNotes
MySQLSTDDEV_SAMP()STDDEV_POP()/STDDEV()/STD()STDDEV() and STD() are synonyms for STDDEV_POP()
PostgreSQLSTDDEV_SAMP() / STDDEV()STDDEV_POP()STDDEV() is a synonym for STDDEV_SAMP()
SQL ServerSTDEV()STDEVP()Does not support STDDEV_POP() or STDDEV_SAMP()
OracleSTDDEV_SAMP() / STDDEV()STDDEV_POP()STDDEV() returns 0 instead of NULL on 1-row scenarios

How to Calculate the Standard Deviation in SQL

Now that you understand SQL standard deviation functions, you are ready to learn how to use them in queries.

Standard SQL (MySQL, PostgreSQL, Oracle)

Here are the syntax structures and explanations for standard SQL standard deviation functions:

  • STDDEV_SAMP(expr) computes the cumulative sample standard deviation and returns the square root of the sample variance.
  • STDDEV_POP(expr) computes the population standard deviation and returns the square root of the population (entire) variance.

Notes:

  • These functions accept any numeric data type or a non-numeric type that can be implicitly converted to a numeric type.
  • The return type matches the numeric data type of the input argument.
  • If there are no matching rows, fewer than two rows, or if expr evaluates to NULL, STDDEV_SAMP() returns NULL while STDDEV_POP() returns 0.
  • They ignore NULL values in the calculation.

SQL Server

The SQL Server standard deviation functions are:

  • STDEV() calculates the statistical standard deviation of all values in the specified expression.
  • STDEVP() calculates the statistical standard deviation for the population for all values in the specified expression.

The syntax of the two aggregate functions is:

Copy
        
1 STDEV|STDEVP([ALL|DISTINCT] expression)

Where:

  • ALL applies the function to all values which is the default behavior
  • DISTINCT specifies that each unique value is considered. Find out more about DISTINCT in SQL.
  • expression must be a numeric SQL Server expression.

Notes:

  • Aggregate functions and SQL subqueries are not permitted as expression values.
  • The functions can be used with numeric columns only.
  • NULL values are ignored.

SQL Standard Deviation: Complete Example

To better understand how standard deviation calculations work in SQL, let’s go through a complete example.

Note 1: The queries below will be executed in DbVisualizer, a visual database client that makes it easier to interpret the results.

Note 2: The sample queries will be written in MySQL and follow standard SQL syntax.

Time for a standard deviation SQL example!

Step #1: Data Presentation

Suppose you have a sales_data table that stores the number of items sold by your e-commerce store on a daily basis. It contains the following sample data:

The sales_data table in DbVisualizer
The sales_data table in DbVisualizer

As you can see, the sales_amount column can be NULL, which could indicate either that there were no sales on that day or that the number of sales is unknown. After all, NULLs are not the same.

See how to apply the standard deviation functions on the sales_amount field!

Step #2: Execute STDDEV_SAMP()

You can calculate the sample standard deviation of the sales_amount field using STDDEV_SAMP() as below:

Copy
        
1 SELECT STDDEV_SAMP(sales_amount) 2 FROM sales_data;

The result will be:

Note the result
Note the result

As you can see, STDDEV_SAMP() automatically ignores NULL values, as expected. Verify that on the Standard Deviation Calculator page:

No NULL values, but same result
No NULL values, but same result

If you want to treat NULL values as 0s instead, you can modify the query like this:

Copy
        
1 SELECT STDDEV_SAMP(COALESCE(sales_amount, 0)) 2 FROM sales_data;

The SQL COALESCE function will replace NULL values with 0 before calculating the standard deviation, leading to a different result:

Note the new result on the same table
Note the new result on the same table

As you can verify in the image below:

This time, there is the 0
This time, there is the 0

Great! Mission complete.

Step #3: Execute STDDEV_SAMP()

Now, assume you want to calculate the population standard deviation. This time, you have to use the STDDEV_POP() function:

Copy
        
1 SELECT STDDEV_POP(sales_amount) 2 FROM sales_data;

The result will be:

Executing the STDDEV_POP query in Dbvisualizer
Executing the STDDEV_POP query in Dbvisualizer

Verify the result with the online standard deviation calculation tool:

The resulting standard deviation matches the DbVisualizers output
The resulting standard deviation matches the DbVisualizer’s output

Fantastic! You now know how to apply standard deviation in SQL.

Conclusion

In this guide, you learned about the standard deviation SQL functions and how to use them to calculate standard deviation in SQL. As shown here, STDDEV_SAMP() and STDDEV_POP() are the two standard deviation functions in ANSI SQL. On the other hand, SQL Server offers STDEV() and STDEVP() to achieve similar results.

Working with numerical operations becomes much easier with a database client that provides a complete visual data representation and supports all functions from the most popular databases worldwide—just like DbVisualizer.

FAQ

How does the SQL STDEV function handle NULL values?

The SQL STDEV function ignores NULL values when calculating standard deviation. It only considers non-null numeric values in the dataset. If all the values are NULL, the result will be NULL.

What is the difference between the STDEVP and STDEV SQL Server functions?

The main difference between the STDEVP() and STDEV() functions in SQL Server is that STDEVP() calculates the population standard deviation, while STDEV() calculates the sample standard deviation. Thus:

  • STDEVP() assumes the data represents the entire population.
  • STDEV() assumes the data is a sample of a larger population.

In other words, STDEVP() divides by the total number of values, while STDEV() divides by one less than the total number of values.

What is the difference between STD vs STDDEV vs STDDEV_POP vs STDDEV_SAMP in MySQL?

FunctionDescriptionPopulation or Sample
STD()Calculates standard deviation (synonym for STDDEV_POP)Population
STDDEV()Synonym for STDDEV_POPPopulation
STDDEV_POP()Calculates the population standard deviationPopulation
STDDEV_SAMP()Calculates the sample standard deviationSample

In MySQL, STD and STDDEV are synonyms for STDDEV_POP, and STDDEV_SAMP is used for sample standard deviation.

What databases support an SQL standard deviation function?

Many popular databases support SQL standard deviation functions, including:

  • MySQL: Supports STDDEV_POP() and STDDEV_SAMP().
  • PostgreSQL: Supports STDDEV() (sample) and STDDEV_POP() (population).
  • SQL Server: Offers STDEV() (sample) and STDEVP() (population).
  • Oracle: Supports STDDEV() (sample) and STDDEV_POP() (population). These functions allow you to calculate standard deviation in SQL.

Why use a database client with numerical operations?

Working with numerical operations like standard deviation is much easier with a visual database client like DbVisualizer. It allows you to explore input data and view results visually, helping you better understand the output. Then, features like query visualization and SQL formatting make it simpler to work with and initiate aggregate functions, enhancing your overall experience and efficiency in analyzing data. Grab a 21-day free trial today!

Dbvis download link img

About the author
Antonello Zanini

Antonello is a software engineer, and often refers to himself as a technology bishop. His mission is to spread knowledge through writing.

The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

How To List Tables In Postgres: Complete Guide

author Leslie S. Gyamfi tags POSTGRESQL 6 min 2025-04-17
title

A Complete Guide to an SQL Recursive Query Expression

author Lukas Vileikis tags MySQL SQL 5 min 2025-04-16
title

SQL EXISTS: Syntax and Use Cases with Examples

author Leslie S. Gyamfi tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-04-14
title

Welcoming Tables with Foreign Keys Into Your Database

author Lukas Vileikis tags MySQL SQL 5 min 2025-04-10
title

Dealing With NULL in SQL: Complete Guide

author Leslie S. Gyamfi tags SQL 7 min 2025-04-09
title

A Complete Guide to the New MySQL 9 VECTOR Data Type

author Antonello Zanini tags MySQL MySQL 9 5 min 2025-04-08
title

Data Backup and Recovery: Implement the Golden Rule in Your Database

author Lukas Vileikis tags Backup Recovery SQL 6 min 2025-04-07
title

How to Delete Table Data in SQL: A Complete Guide

author Lukas Vileikis tags SQL 5 min 2025-04-03
title

SQL Query Optimization: Everything You Need to Know

author Leslie S. Gyamfi tags OPTIMIZATION SQL 9 min 2025-04-02
title

A Complete Guide to the Different Types of DBMS

author Antonello Zanini tags DBMS NOSQL SQL 8 min 2025-03-31

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.