To demonstrate the differences between ROW_NUMBER, RANK, and DENSE_RANK In SQL Server I have chosen an Employee table that has two employees with the same salary. The following three functions are required for an ORDER BY expression in the OVER clause:
- -- create table
- CREATE TABLE Employee
- (
- Names VARCHAR(10),
- Salary INT
- )
- GO
- -- insert data
- INSERT INTO dbo.Employee
- VALUES ('Rony',10000),('Joy',9000),('Devid',8000),('Warner',7000),('Elly',7000),('Frenil',6000)
- GO
- select
- *,
- Row_Number() over (order by salary desc) RowNumber,
- rank() over (order by salary desc) RankId,
- dense_rank() over (order by salary desc) DenseRank from Employee

Row_Number() will generate a unique number for every row, even if one or more rows has the same value.
RANK() will assign the same number for the row which contains the same value and skips the next number.
DENSE_RANK () will assign the same number for the row which contains the same value without skipping the next number.
To understand the above example, here I have given a simple explanation.
Let's insert one more employee with the same salary. Employee Name is Tod and salary is 7000.
- ---Insert one more employee with same salry as Warner and Elly with 7000
- INSERT INTO dbo.Employee
- VALUES ('Tod',7000)
- GO


Hanifa ArrumaishaPosted Sep 29, 2021, 5:15 PM
Thank you for your explanation. I've been across every page and can't find the use case when it's usefull to choose rank, instead of dense rank. Could you give me any real use case for using rank instead of dense rank? and the opposite case?
Pham QuynhPosted Aug 19, 2020, 11:38 PM
Very useful post. thanks you so much.
Bharat SinghPosted Apr 2, 2019, 8:25 AM
Very nice post. easy to understand. Thanks a lot.
Hadshana KamalanathanPosted Jul 15, 2018, 12:57 PM
Thank you for sharing
Dipak ZalaPosted Jun 21, 2018, 4:21 AM
Nice Article
Prakash ChasiyaPosted Jun 20, 2018, 11:31 PM
Nice explanation. Thanks.