sqlstreak

SQL compared

ROW_NUMBER vs RANK vs DENSE_RANK

Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)

Short answer

All three number rows in ORDER BY order; they differ only on ties. ROW_NUMBER always counts on (1, 2, 3), RANK gives tied rows the same number and then skips (1, 1, 3), and DENSE_RANK gives the same number without a gap (1, 1, 2).

Side by side

ROW_NUMBERRANKDENSE_RANK
Tied rows getDifferent numbers (in no fixed order)The same numberThe same number
After a tieContinues: 1, 2, 3Skips: 1, 1, 3No gap: 1, 1, 2
Highest numberThe number of rowsAt most the number of rowsThe number of distinct values
Typical useExactly one row per group, pagingCompetition-style ranking, top N with tiesThe Nth distinct value

The same data, each way

Every query below was run on PostgreSQL and SQLite when this page was built; the tables are what came back.

All three on the same rows

Pets ranked by age, youngest first. Three pets are 1 year old and four are 2: watch what each function does with them.

SELECT name, age,
  ROW_NUMBER() OVER (ORDER BY age, name) AS row_num,
  RANK()       OVER (ORDER BY age) AS rnk,
  DENSE_RANK() OVER (ORDER BY age) AS dense_rnk
FROM pets
ORDER BY age, name
LIMIT 8;

Result (PostgreSQL and SQLite, identical): 8 rows

nameagerow_numrnkdense_rnk
Bean1111
Pepper1211
Waffles1311
Juniper2442
Kiwi2542
Maple2642
Mochi2742
Biscuit3883

Try it: PostgreSQL playground · SQLite playground

DENSE_RANK finds the 2nd youngest age

Ages ranked without gaps, so rank 2 is the second-smallest age, however many pets share the first.

SELECT DISTINCT age
FROM (SELECT age, DENSE_RANK() OVER (ORDER BY age) AS r
      FROM pets) ranked
WHERE r = 2;

Result (PostgreSQL and SQLite, identical): 1 row

age
2

Try it: PostgreSQL playground · SQLite playground

ROW_NUMBER keeps exactly one row per group

The youngest pet of each species; name breaks ties so the pick is stable.

SELECT species, name, age
FROM (SELECT species, name, age,
        ROW_NUMBER() OVER (PARTITION BY species ORDER BY age, name) AS rn
      FROM pets) t
WHERE rn = 1
ORDER BY species;

Result (PostgreSQL and SQLite, identical): 4 rows

speciesnameage
birdKiwi2
catPepper1
dogBean1
rabbitWaffles1

Try it: PostgreSQL playground · SQLite playground

When to use which

ROW_NUMBER

When you need exactly one row per group (the latest order, the cheapest item) or stable page numbers. Add a unique tie-breaker to ORDER BY.

RANK

When ties should share a place and the next place should reflect how many came before, like a race: 1, 1, 3.

DENSE_RANK

When you want the Nth distinct value (the second-highest price) or tiers without gaps.

Common mistakes

  • ROW_NUMBER without a unique tie-breaker: tied rows get their numbers in an arbitrary order that can change between runs.

  • RANK for “the second-highest value”: after a tie at 1 there is no rank 2. Use DENSE_RANK.

  • Filtering on the number in WHERE: window functions run after WHERE. Wrap the query in a subquery or CTE and filter outside.

PostgreSQL and SQLite

Everything on this page works the same in PostgreSQL and SQLite: each example above was run on both, and the results match.

Official documentation

Go deeper

Make it stick in five minutes a day

“Ranking with OVER” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.

Start free