Solve “Deduplicate events with ROW_NUMBER”

Free. Runs in your browser, no install.

SQL · Window Functions

ROW_NUMBER vs RANK vs DENSE_RANK

Mediumrow_numberrankdense_rank

Interview question

What is the difference between ROW_NUMBER, RANK, and DENSE_RANK?

How to answer

The short answer

Suppose salaries are:

Output
100
100
90
80

Results:

Output
ROW_NUMBER:   1  2  3  4
RANK:         1  1  3  4
DENSE_RANK:   1  1  2  3

ROW_NUMBER

Every row gets a unique number.

RANK

Ties receive the same rank, and gaps are created.

DENSE_RANK

Ties receive the same rank, but no gaps.

Lock it in with a real drill

You remember an answer longer once you have used it. Try a hands-on problem on this concept.

Open related drill