ROW NUMBER, RANK and DENSE RANK in SQL

By | December 14, 2017

In this post, We will learn the difference between Row_Number, Rank, Dense_Rank and how to use with example.

ROW_NUMBER, RANK and DENSE_RANK functions are used to calculate RowID for the result set which is returned from a query but in a slightly different way.

Firstly, We will create a table and insert some record for implementing these functions as below:

Row_Number():

Row_Number function will assign a unique id to each row which is returned from the query.

Example:

 

Rank():

Rank function will assign a unique number to each distinct row, but it leaves a gap between the groups.

Example:

 

Dense_Rank():

Dense_Rank function is similar to Rank with only difference is that, it will not leave gaps between groups. So if we use the same query used for Rank, then:

 

I hope you will enjoy the use of ROW_NUMBER, RANK and DENSE_RANK in SQL. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Like it? Share it
  • ritesh kumar singh

    Nice, but example of Rank and Rownumber should interchange.