The SQL VARP Function calculates the Statistical Variance for the population of total rows selected by the SQL SELECT Statement. The syntax of the SQL Server VARP Function is

SELECT VARP([Column_Name]) FROM [Source]

## SQL VARP Function Formula

SQL VARP Function only work on Numeric Columns, and it ignores Nulls. The math formulas behind the VARP function to calculate the Statistical Variance for the population in SQL Server is

--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

For this Function, We use the below shown data

## SQL VARP Example

It returns the Variance for the population of the total number of records present in the specified column. For example, the following SQL Server query will calculate the variance of total records present in the [Yearly Income] column from the Customers table.

SELECT VARP([YearlyIncome]) AS [Income Variance] FROM [Customer]

## SQL VARP Function with Group By Clause

In most cases, we usually calculate the variance of products belongs to a particular category. In these situations, we use GROUP BY Clause to group the products by category. And then, use this Function to calculate the variance for the population of products present in each group. Let us see the Aggregate Function Example

SELECT [Occupation] ,VARP([YearlyIncome]) AS [Income Variance] FROM [Customer] GROUP BY [Occupation]

Above SQL Select Statement Query will find, the Customers associated with the particular Department and calculates their income variance

We are taking the Skilled Manual profession and show you the output.

-- Calculating Mean Mean = (60000 + 80000) / 2 Mean = 70000 --Calculating variance for Population Variance for population = ( (60000 - 70000) + (80000 - 70000) ) / 2 Variance for population = 100000000

### In Having Clause

When we are grouping the data, In some cases, we usually check for the conditions against the aggregated data. In these circumstances, we use the SQL HAVING Clause along with Group By Statement. For instance, the following query group the Customers by their Occupation and then finds the Income variance of each group.

SELECT [Occupation] ,VARP([YearlyIncome]) AS [Income Variance] FROM [Customer] GROUP BY [Occupation] HAVING VARP ([YearlyIncome]) > 0

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

HAVING VARP([YearlyIncome]) > 0