Skip to main content

VAR function in SQL Server

The VAR is an aggregate function in SQL Server which returns the variance of all or distinct values of the specified column in SQL Statement. 
Let's look into the syntax,

Syntax

SELECT VAR ( [ ALL | DISTINCT ] expression )
FROM [Table_Name]

ALL - It is default. Returns the variance of all values.

DISTINCT - Returns the variance of unique values

ALL

SELECT VAR(ALL ProductID) as ALLProduct 
FROM [AdventureWorks2019].[Production].[ProductInventory]

The above statement returns the variance of ALL product values.

DISTINCT

SELECT VAR(DISTINCT ProductID) as DistinctProduct 
FROM [AdventureWorks2019].[Production].[ProductInventory]

The above statement returns the variance of DISTINCT product values.



To learn more - Click Here


Comments