ROW_NUMBER() , RANK() and DENSE_RANK() are SQL window functions that number rows in the order you give them. They only differ when two rows tie:
ROW_NUMBER() still gives each row its own number,
RANK() gives tied rows the same number and then skips ahead (1, 2, 2, 4),
DENSE_RANK() gives them the same number and carries on without a gap (1, 2, 2, 3).
The difference in one table
Take five students with scores 95, 90, 90, 85 and 80, ordered from highest to lowest.
Function Tied rows get Gap after a tie Scores 95, 90, 90, 85 Typical use
ROW_NUMBER() Different numbers No 1, 2, 3, 4 One row per group, deduplication
RANK() The same number Yes 1, 2, 2, 4 Leaderboards, competition ranking
DENSE_RANK() The same number No 1, 2, 2, 3 Nth-highest distinct value
Try it yourself
Paste these tables into the Tables box of a SQL editor (I use the free SQL online compiler on PySpark.in : MySQL 8, no sign-up):
CREATE TABLE scores ( student VARCHAR ( 20 ) , score INT ) ; INSERT INTO scores VALUES ( 'Asha' , 95 ) , (…