# Rank and Dense Rank in SQL Server

rank and dense_rank example

difference between rank and dense_rank with example

rank vs dense_rank in sql server 2008

sql server difference between rank and dense_rank

In this video we will discuss Rank and Dense_Rank functions in SQL Server

Rank and Dense_Rank functions

Introduced in SQL Server 2005

Returns a rank starting at 1 based on the ordering of rows imposed by the ORDER BY clause

ORDER BY clause is required

PARTITION BY clause is optional

When the data is partitioned, rank is reset to 1 when the partition changes

Difference between Rank and Dense_Rank functions

Rank function skips ranking(s) if there is a tie where as Dense_Rank will not.

For example : If you have 2 rows at rank 1 and you have 5 rows in total.

RANK() returns – 1, 1, 3, 4, 5

DENSE_RANK returns – 1, 1, 2, 3, 4

Syntax :

RANK() OVER (ORDER BY Col1, Col2, …)

DENSE_RANK() OVER (ORDER BY Col1, Col2, …)

RANK() and DENSE_RANK() functions without PARTITION BY clause : In this example, data is not partitioned, so RANK() function provides a consecutive numbering except when there is a tie. Rank 2 is skipped as there are 2 rows at rank 1. The third row gets rank 3.

DENSE_RANK() on the other hand will not skip ranks if there is a tie. The first 2 rows get rank 1. Third row gets rank 2.

SELECT Name, Salary, Gender,

RANK() OVER (ORDER BY Salary DESC) AS [Rank],

DENSE_RANK() OVER (ORDER BY Salary DESC) AS DenseRank

FROM Employees

RANK() and DENSE_RANK() functions with PARTITION BY clause : Notice when the partition changes from Female to Male Rank is reset to 1

SELECT Name, Salary, Gender,

RANK() OVER (PARTITION BY Gender ORDER BY Salary DESC) AS [Rank],

DENSE_RANK() OVER (PARTITION BY Gender ORDER BY Salary DESC) AS DenseRank

FROM Employees

Use case for RANK and DENSE_RANK functions : Both these functions can be used to find Nth highest salary. However, which function to use depends on what you want to do when there is a tie. Let me explain with an example.

If there are 2 employees with the FIRST highest salary, there are 2 different business cases

1. If your business case is, not to produce any result for the SECOND highest salary, then use RANK function

2. If your business case is to return the next Salary after the tied rows as the SECOND highest Salary, then use DENSE_RANK function

Since we have 2 Employees with the FIRST highest salary. Rank() function will not return any rows for the SECOND highest Salary.

WITH Result AS

(

SELECT Salary,

RANK() OVER (ORDER BY Salary DESC) AS Salary_Rank

FROM Employees

)

SELECT TOP 1 Salary FROM Result WHERE Salary_Rank = 2

Though we have 2 Employees with the FIRST highest salary. Dense_Rank() function returns, the next Salary after the tied rows as the SECOND highest Salary

WITH Result AS

(

SELECT Salary,

DENSE_RANK() OVER (ORDER BY Salary DESC) AS Salary_Rank

FROM Employees

)

SELECT TOP 1 Salary FROM Result WHERE Salary_Rank = 2

You can also use RANK and DENSE_RANK functions to find the Nth highest Salary among Male or Female employee groups. The following query finds the 3rd highest salary amount paid among the Female employees group

WITH Result AS

(

SELECT Salary, Gender,

DENSE_RANK() OVER (PARTITION BY Gender ORDER BY Salary DESC) AS Salary_Rank

FROM Employees

)

SELECT TOP 1 Salary FROM Result

WHERE Salary_Rank = 3 AND Gender = ‘Female’

Text version of the video

http://csharp-video-tutorials.blogspot.com/2015/10/rank-and-denserank-in-sql-server.html

Slides

http://csharp-video-tutorials.blogspot.com/2015/10/rank-and-denserank-in-sql-server_1.html

All SQL Server Text Articles

http://csharp-video-tutorials.blogspot.com/p/free-sql-server-video-tutorials-for.html

All SQL Server Slides

http://csharp-video-tutorials.blogspot.com/p/sql-server.html

All Dot Net and SQL Server Tutorials in English

https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd

All Dot Net and SQL Server Tutorials in Arabic

https://www.youtube.com/c/KudvenkatArabic/playlists

## Social