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_NUMBER | RANK | DENSE_RANK | |
|---|---|---|---|
| Tied rows get | Different numbers (in no fixed order) | The same number | The same number |
| After a tie | Continues: 1, 2, 3 | Skips: 1, 1, 3 | No gap: 1, 1, 2 |
| Highest number | The number of rows | At most the number of rows | The number of distinct values |
| Typical use | Exactly one row per group, paging | Competition-style ranking, top N with ties | The 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
| name | age | row_num | rnk | dense_rnk |
|---|---|---|---|---|
| Bean | 1 | 1 | 1 | 1 |
| Pepper | 1 | 2 | 1 | 1 |
| Waffles | 1 | 3 | 1 | 1 |
| Juniper | 2 | 4 | 4 | 2 |
| Kiwi | 2 | 5 | 4 | 2 |
| Maple | 2 | 6 | 4 | 2 |
| Mochi | 2 | 7 | 4 | 2 |
| Biscuit | 3 | 8 | 8 | 3 |
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
| species | name | age |
|---|---|---|
| bird | Kiwi | 2 |
| cat | Pepper | 1 |
| dog | Bean | 1 |
| rabbit | Waffles | 1 |
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
- Guide: SQL window functions
- Practice: SQL window functions practice: 8 exercises with answers
- Lesson: Ranking with OVER (Pro)
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.