The SQL STDEV Function is one of the SQL Aggregate Function, which is is used to calculate the Standard Deviation of total records (or rows) selected by the SELECT Statement.

**TIP:** STDEV Function will only work on Numeric Columns and it ignores Nulls. — SQL Server STDEV Function example

## SQL STDEV Function Formula

The mathematical formulas behind the STDEV function to calculate the standard deviation in SQL Server is as shown below:

--Calculating the Mean or Average Mean = Sum of each individual/Total number of items --Calculating the Statistical Variance Variance = ((OriginalValue – Mean)² + (OriginalValue – Mean)² +.... )/(Total number of items - 1) --Calculating Standard Deviation Standard Deviation = Square root (Variance)

### SQL STDEV Function Syntax

The basic syntax of the STDEV in SQL Server is as shown below:

SELECT STDEV ([Column_Name]) FROM [Source]

In this article we will show you, How to write STDEV in SQL Server to calculate the Standard Deviation in Sql Server with example. For this, We are going to use the below shown data

## SQL STDEV Example

The STDEV in SQL simply returns the Standard Deviation of total number of records present in the specified column. For example, The following query will calculate the Standard Deviation of total records present in the [Yearly Income] column from Customers table.

**T-SQL CODE**

-- SQL Server STDEV Function example SELECT STDEV ([YearlyIncome]) AS [Standard Deviation] FROM [Customer]

**OUTPUT**

## SQL STDEV Function with Group By Clause

In most cases we usually calculate the Standard Deviation of a products belongs to particular category or color etc. In these situations we use SQL GROUP BY Clause to group the products by color or category and then use the STDEV Function to calculate the Standard Deviation of products present in each group. Let us see the Example

-- SQL Server STDEV Function example SELECT [Occupation] ,STDEV ([YearlyIncome]) AS [Standard Deviation] FROM [Customer] GROUP BY [Occupation]

Above SQL Query will find, the Customers associated with the particular Department and calculates their Standard Deviation

**OUTPUT**

**ANALYSIS**

It is too big to show the calculations for every thing so, We are taking Skilled Manual profession and show you the output.

--Calculating Mean Mean = (60000 + 80000) / 2 Mean = 70000 --Calculating Variance Variance = ( (60000 - 70000) + (80000 - 70000) ) / (2 -1) Variance = 200000000 --Calculating Standard Deviation Standard Deviation = SQRT (Variance) Standard Deviation = SQRT (200000000) which means, Standard Deviation = 14142.13

## SQL STDEV Function in Having Clause

When we are grouping the data, In some cases we usually check for the conditions against the aggregated data. In these situations we use SQL HAVING Clause along with Group By Statement.

For example, The following Sql Server stdev query will group the Customers by their Occupation and then calculates the Standard Deviation of each individual group.

**T-SQL CODE**

-- SQL Server STDEV Function example SELECT [Occupation] ,STDEV ([YearlyIncome]) AS [Standard Deviation] FROM [Customer] GROUP BY [Occupation] HAVING STDEV ([YearlyIncome]) > 0

Below lines of code will check whether the aggregated amount (Standard Deviation of Yearly income for each individual Group) is greater than 0 or not. If this is True then corresponding records will be displayed.

HAVING STDEV ([YearlyIncome]) > 0

**OUTPUT**

## SQL STDEV Function in Subquery

In this example, we will show you, how to calculate standard Deviation in Sql Server using the subquery.

When we are retrieving the data, In some cases we usually check for the conditions against the aggregated data. In these situations we have to use Subqueries in Where Clause.

For example, The following query will return all the Customers present in the customers table whose [Yearly Income] is greater than five times yearly incomes Standard Deviation in Sql Server.

-- SQL Server STDEV Function example SELECT [FirstName] ,[LastName] ,[YearlyIncome] ,[Education] ,[Occupation] FROM [Customer] WHERE [YearlyIncome] > (SELECT STDEV([YearlyIncome] * 5) FROM [Customer])

From the first example you can observe that, Standard Deviation of a Yearly Income is 12516. So above query will display all the customers from the Customers table whose yearly income is greater than 62580 approximately

**OUTPUT**

Thank You for Visiting Our Blog