Understanding the VAR() Function in SQL Server

In SQL Server the VAR() function returns the statistical variance of all values in the specified expression.

Syntax

The function can be used as an aggregate function or as an analytical/window function.

When used as an aggregate function, the syntax goes like this:

VAR ( [ ALL | DISTINCT ] expression )

And as an analytical function:

VAR ([ ALL ] expression) OVER ( [ partition_by_clause ] order_by_clause)

The expression must be either an exact numeric or approximate numeric data type, except for the bit data type.

Example

Suppose we have a table like this:

SELECT * FROM Dogs;

Result:

DogName  Activity     Score
-------  -----------  -----
Bruno    Fetch Stick  43   
Cooper   Fetch Stick  43   
Max      Fetch Stick  91   
Bruno    Wag Tail     65   
Cooper   Wag Tail     51   
Max      Wag Tail     87   
Bruno    Keep Quiet   3    
Cooper   Keep Quiet   null 
Max      Keep Quiet   1    
null     Catch Cat    null 

We can use the VAR() function against the Score column like this:

SELECT VAR(Score) FROM Dogs;

Result:

1136

The VAR() function ignores NULL values and so these aren’t factored into the results.

The DISTINCT Argument

By default, all rows are evaluated, regardless of whether they contain unique values. However, we can use the DISTINCT keyword to specify that only distinct (unique) values are evaluated:

SELECT VAR(DISTINCT Score) FROM Dogs;

Result:

1320.5714285714287

We can see that this has changed the result slightly. This is because our underlying data contains duplicates – two rows contain 43. When we used the DISTINCT keyword, the VAR() function treated those two as one. This resulted in a different statistical variance.

The ALL Argument

By default, all rows are evaluated regardless of whether they contain duplicates. In other words, this is the case when we don’t use the DISTINCT argument to eliminate duplicates from evaluation.

However, we also have the option of using the ALL keyword to explicitly specify that all values are evaluated:

SELECT VAR(ALL Score) FROM Dogs;

Result:

1136

We can see that this result is the same as the first example. That’s because the functionality is exactly the same when we use the ALL keyword.

The OVER Clause

We can include an OVER clause in order to use VAR() as a window function.

Example:

SELECT 
    Activity,
    DogName,
    Score,
    VAR(Score) OVER(ORDER BY Activity) AS "VAR"
FROM Dogs;

Result:

Activity     DogName  Score  VAR   
-----------  -------  -----  ------
Catch Cat    null     null   null  
Fetch Stick  Bruno    43     768   
Fetch Stick  Cooper   43     768   
Fetch Stick  Max      91     768   
Keep Quiet   Bruno    3      1359.2
Keep Quiet   Cooper   null   1359.2
Keep Quiet   Max      1      1359.2
Wag Tail     Bruno    65     1136  
Wag Tail     Cooper   51     1136  
Wag Tail     Max      87     1136  

Here, I used an ORDER BY clause in the OVER clause to specify the logical order in which the operation is performed.

We could also put another ORDER BY clause at the end of the query in order to determine how the result set is sorted.

Partitioning the Results

The OVER clause also accepts a PARTITION BY clause that allows us to partition the results by a given column.

Here’s an example:

SELECT 
    Activity,
    DogName,
    Score,
    VAR(Score) OVER(PARTITION BY Activity) AS "VAR"
FROM Dogs;

Result:

Activity     DogName  Score  VAR               
-----------  -------  -----  ------------------
Catch Cat    null     null   null              
Fetch Stick  Bruno    43     768               
Fetch Stick  Cooper   43     768               
Fetch Stick  Max      91     768               
Keep Quiet   Bruno    3      2                 
Keep Quiet   Cooper   null   2                 
Keep Quiet   Max      1      2                 
Wag Tail     Bruno    65     329.33333333333303
Wag Tail     Cooper   51     329.33333333333303
Wag Tail     Max      87     329.33333333333303

In this case we partitioned by the Activity column, and so we got the statistical variance within each activity.

Here it is with an ORDER BY clause added to the OVER clause:

SELECT 
    Activity,
    DogName,
    Score,
    VAR(Score) OVER(PARTITION BY Activity ORDER BY Score) AS "VAR"
FROM Dogs;

Result:

Activity     DogName  Score  VAR               
-----------  -------  -----  ------------------
Catch Cat    null     null   null              
Fetch Stick  Bruno    43     0                 
Fetch Stick  Cooper   43     0                 
Fetch Stick  Max      91     768               
Keep Quiet   Cooper   null   null              
Keep Quiet   Max      1      null              
Keep Quiet   Bruno    3      2                 
Wag Tail     Cooper   51     null              
Wag Tail     Bruno    65     98                
Wag Tail     Max      87     329.33333333333303

No DISTINCT Clause with the OVER Clause

We can’t use the DISTINCT clause when using the OVER clause.

Here’s what happens when we add the DISTINCT clause to the previous example:

SELECT 
    Activity,
    DogName,
    Score,
    VAR(DISTINCT Score) OVER(PARTITION BY Activity ORDER BY Score) AS "VAR"
FROM Dogs;

Result:

Msg 10759, Level 15, State 1, Line 5
Use of DISTINCT is not allowed with the OVER clause.

However, we can use the ALL clause if we want.