Rank and Dense Rank in SQL Server

preview_player
Показать описание
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

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

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

Slides

All SQL Server Text Articles

All SQL Server Slides

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic
Рекомендации по теме
Комментарии
Автор

Venkat rules! I'm the biggest fan of your tutorials. You are the best teacher in the world. God bless you for all your work and achievements. I learned a lot from your videos. Your explanations are fantastic and the way of your teaching is superb. Greatings from Poland !

krzysztofs
Автор

I have said this before about you, but it bears repeating. You are the foremost expert on grouping data. I appreciate your videos and they have helped me a lot.

spaces
Автор

Everytime I watch venkat tutorials, I like the video first and then I start. 👍

dhamma
Автор

Hi Venkat,
I wanted to take a moment to express my heartfelt gratitude for your exceptional teaching of programming. Your dedication and expertise have had a tremendous impact on my learning journey, and I am incredibly grateful for your guidance.

Your ability to break down complex programming concepts into understandable chunks truly impressed me. Through your clear explanations and engaging teaching style, you made learning programming enjoyable and accessible.

chetanchaudhari
Автор

Thank you for all your tutorials..I love your explanations and your soothing voice.I wish I had your brain, you make it look so easy, lol.

poorvi
Автор

Omg...you are an amazing teacher. I feel too bad about not finding your tutorials until now. THANK YOU!

srujanareddy
Автор

Great explanation. One of the very few tutorials I have seen which is very clear and still very precise. Super job! Thanks Venkat

rajreddy
Автор

thanks for all your tutorials and clear explanations

liverickoben
Автор

Although this is an old video the explanation is a real gem. Timeless knowledge

gurjeet
Автор

Thanks a lot for ur explanation.The highlight is the clarity and simplicity of explanation

sumaiyamohiyuddin
Автор

This was the best explanation ever. Thank you for that!

awengirr
Автор

thanks. this is the best explanation that I have seen for this.

dbin
Автор

This helped me understand Rank better. Thank you.

mariorivera
Автор

Super awesome, your explanations are truly good. I really learnt a lot from it....Thank you so much!!

deepakpangali
Автор

Great Videos and clear Explanation.Thanks a lot!!! it is very useful

pavithravvenu
Автор

hats off to you.. you are a great teacher..

soniasadeque
Автор

Thanks very much for these videos.They are simple to understand and really helpful in learning query writing.

absneo
Автор

you guys are great! I learned a lot from your videos

lilarasekh
Автор

Thanks Professor..tutorial is quite helpful.

TheSwatirock
Автор

Hello sir, pls make video on table partitioning and their concept...this is something important which is not made yet

samirkumar