Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Difference between ROW_NUMBER and RANK

Vivek LApr 28 2011 — edited Apr 28 2011
Hi,

Just want to know, is there any other signifacnt difference in behaviour of RANK and ROW_NUMBER other then in an event of a tie?
As far as I know, RANK rates two rows as eqaul in an event of tie, whereas ROW_NUMBER treats them as different with unique running serial number provided to each record.

here is some sample code that I worked upon :

For RANK :
WITH T AS (
 SELECT 'EMP1' NAME, 1000 SAL FROM DUAL
 UNION
 SELECT 'EMP2', 200 FROM DUAL
 UNION 
 SELECT 'EMP3', 500 FROM DUAL
 UNION
 SELECT 'EMP4', 1000 FROM DUAL

)
SELECT NAME,
       SAL,
       RANK() OVER (ORDER BY SAL DESC ) "rank"
FROM   T
ORDER BY SAL ;
Output went like :
NAME SAL rank 
EMP2 200 4 
EMP3 500 3 
EMP1 1000 1 
EMP4 1000 1 
whereas with
ROW_NUMBER
WITH T AS (
 SELECT 'EMP1' NAME, 1000 SAL FROM DUAL
 UNION
 SELECT 'EMP2', 200 FROM DUAL
 UNION 
 SELECT 'EMP3', 500 FROM DUAL
 UNION
 SELECT 'EMP4', 1000 FROM DUAL

)
SELECT NAME,
       SAL,
       ROW_NUMBER() OVER (ORDER BY SAL DESC ) "rank"
FROM   T
ORDER BY SAL;
outout comes as
NAME SAL rank 
EMP2 200 4 
EMP3 500 3 
EMP1 1000 1 
EMP4 1000 2 
This post has been answered by 780914 on Apr 28 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 26 2011
Added on Apr 28 2011
4 comments
20,558 views