SQL VAR Function

The SQL Server VAR Function is an Aggregate Function, which is used to determine the statistical Variance of entire records (or rows) selected by the SELECT Statement. The syntax of the VAR() Function is

SELECT VAR ([Column_Name])
FROM [Source]

SQL VAR Function Formula

The VAR Function will only work on Numeric Columns, and it ignores Nulls. The mathematical formulas behind the function to calculate the Sql Server Statistical Variance are.

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

For this VAR function demo, we use the below-shown data

Customer table

SQL Server VAR function Example

The VAR returns the Variance of the total number of records present in the specified column. For example, the following query will calculate the variance of total records present in the [Yearly Income] column from the Customers table. Please refer to the SQL Aggregate Functions list from the SQL language basics.

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

Using SQL VAR() with DISTINCT

The VAR() function in the query below uses the DISTINCT keyword to calculate the statistical variance of unique YearlyIncome records in a customer table. Refer to SQL SELECT DISTINCT.

SELECT VAR(DISTINCT [YearlyIncome]) AS [Result]
FROM [Customer]
Result
-------
304166666.666666

Similarly, use VAR() to calculate the statistical variance of unique yearly income values in each occupation group.

SELECT [Occupation]
,VAR(DISTINCT [YearlyIncome]) AS Result
FROM [Customer]
GROUP BY [Occupation]
Occupation	Result
Clerical	12500000
Management	50000000
Professional	100000000
Skilled Manual	50000000

SQL Variance with Group By Clause

In most cases, we usually calculate the variance of products belonging to a particular category or color, etc. In these situations, we use the SQL Server GROUP BY to group the products by color or category. Then use the VAR Function to calculate the variance of products present in each group. Please refer to the SQL VARP and SQL SUM functions.

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

The above SQL Server Query will find the Customers associated with the particular Department and calculate their income variance. Refer to the SQL AVG function.

SQL VAR FUNCTION With Group By Clause 2

It is too big to show the Mean and Variance calculations for everything. So, We are taking the Skilled Manual profession and showing you the output.

–Calculating Mean Mean = (60000 + 80000) / 2 Mean = 70000 –Calculating Variance Variance = ( (60000 – 70000) + (80000 – 70000) ) / (2 -1) Variance = 200000000

SQL VAR 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 the SQL HAVING Clause along with the Group By Statement. For example, the following SQL Server SELECT query will group the Customers by their Occupation and then find the Income variance of each group.

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

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

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

Using VAR() with OVER(PARTITION BY)

The query below uses VAR() with the OVER clause and PARTITION BY to partition customers by education and calculate the statistical variance of customers’ yearly income.

SELECT [FirstName],[LastName]
,[Education]
,[YearlyIncome]
,VAR([YearlyIncome]) OVER(PARTITION BY [Education]) AS Result
FROM [Customer]
FirstName	LastName	Education	YearlyIncome	Result
John	Yang	Bachelors	90000.00	66666666.6666667
Rob	Johnson	Bachelors	80000.00	66666666.6666667
Christy	Zhu	Bachelors	80000.00	66666666.6666667
John	Ruiz	Bachelors	70000.00	66666666.6666667
Christy	Carlson	Graduate Degree	70000.00	NULL
Rob	Huang	High School	60000.00	NULL
John	Miller	Masters Degree	80000.00	NULL
Ruben	Torres	Partial College	50000.00	NULL
Christy	Mehta	Partial High School	50000.00	12500000
Rob	Verhoff	Partial High School	45000.00	12500000
Categories SQL