SQL: RANK skips numbers after a tie, DENSE_RANK does not

Three functions, three answers on ties: ROW_NUMBER always increments, RANK leaves gaps, DENSE_RANK does not. Picking the wrong one shows up only when two rows are equal.

Code
SELECT name, score,
       ROW_NUMBER() OVER (ORDER BY score DESC) AS rownum,
       RANK()       OVER (ORDER BY score DESC) AS rnk,
       DENSE_RANK() OVER (ORDER BY score DESC) AS dense
  FROM results;
Output
name | score | rownum | rnk | dense
Ann  |   100 |      1 |   1 |     1
Bob  |   100 |      2 |   1 |     1
Carl |    90 |      3 |   3 |     2
Dee  |    80 |      4 |   4 |     3

-- ROW_NUMBER gave Ann and Bob different numbers on identical scores, which
-- is arbitrary - and not stable between runs unless you add a tie-break.
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-25