SQL VARP Function

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

SELECT VARP([Column_Name])
FROM [Source]

SQL Server VARP Function only works on Numeric Columns, and it ignores Nulls. The math formulas behind the VARP function to calculate the Statistical Variance for the population are 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

For this VARP Function, We use the below shown data.

Customer Table

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. See the list of SQL Server Aggregate Functions from the SQL Programming page.

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

In the query below, we use VARP() with the DISTINCT keyword to find the statistical variance for the population of unique customers. Refer to the SQL SELECT DISTINCT.

SELECT VARP(DISTINCT [YearlyIncome]) AS Result
FROM [Customer]
253472222.222222

SQL VARP Function with Group By Clause

In most cases, we usually calculate the variance of products belonging to a particular category. In these situations, we use the GROUP BY Clause in SQL to group the products by category. And then, use this Function to calculate the variance for the population of products present in each group. Please refer to the SQL VAR function.

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

The above SQL SELECT Statement Query will find, the Customers associated with the particular Department and calculate their income variance

VARP FUNCTION 2

We are taking the Skilled Manual profession and showing you the output. Also, refer to the SQL Server SUM function and SQL AVG function articles..

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

VARP 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 the Group By Statement. For instance, the following query groups 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

The 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 are displayed.

HAVING VARP([YearlyIncome]) > 0
VARP FUNCTION 3

Using the VARP() function with OVER(PARTITION BY)

The query below uses the VARP() function with the OVER clause and PARTITION BY to partition customers by occupation and calculates the statistical variance for the population of customers’ income.

SELECT CONCAT([FirstName], ' ', [LastName]) AS FullName
,[Occupation]
,[YearlyIncome]
,VARP([YearlyIncome]) OVER(PARTITION BY [Occupation]) AS Result
FROM [Customer]
FullName	Occupation	YearlyIncome	Result
Christy Mehta	Clerical	50000.00	6250000
Rob Verhoff	Clerical	45000.00	6250000
Christy Carlson	Management	70000.00	22222222.2222226
Rob Johnson	Management	80000.00	22222222.2222226
John Miller	Management	80000.00	22222222.2222226
Christy Zhu	Professional	80000.00	66666666.6666667
John Yang	Professional	90000.00	66666666.6666667
John Ruiz	Professional	70000.00	66666666.6666667
Rob Huang	Skilled Manual	60000.00	25000000
Ruben Torres	Skilled Manual	50000.00	25000000
Categories SQL