Moonfrog Interview Questions
Question
What are the differences among ROWNUM, RANK and DENSE_RANK? (Standard Question)
in progress
0
Statistics
55 years
2 Answers
996 views
Great Grand Master 0
Answers ( 2 )
RowNum() assigns a unique number to every row starting from 1 and incrementing by 1 for every row.
Rank() – Assigns a Rank for a particular column. It assigns equal rank for 2 same values and skips the next rank
for the next value in order. So, if there are 2 highest values, it will assign 1,1,3 and so on.
Dense_Rank() – This function doesn’t skip the rank so in the same case as above, it will assign 1,1,2 and so on.
The only difference between RANK, DENSE_RANK and ROW_NUMBER function is when there are duplicate values in the column being used in ORDER BY Clause.
RANK function skips the next N-1 ranks if there is a tie between N previous ranks. On the other hand, the DENSE_RANK function does not skip ranks if there is a tie between ranks. Finally, the ROW_NUMBER function has no concern with ranking. It simply returns the row number of the sorted records. Even if there are duplicate records in the column used in the ORDER BY clause, the ROW_NUMBER function will not return duplicate values. Instead, it will continue to increment irrespective of the duplicate values