# ROW_NUMBER vs RANK vs DENSE_RANK

Source: https://sqlstreak.com/sql/rank-vs-dense-rank-vs-row-number · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


**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 |

## Examples

### 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.

```sql
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 (both engines):

| 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 |

### 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.

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

Result (both engines):

| age |
| --- |
| 2 |

### ROW_NUMBER keeps exactly one row per group

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

```sql
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 (both engines):

| species | name | age |
| --- | --- | --- |
| bird | Kiwi | 2 |
| cat | Pepper | 1 |
| dog | Bean | 1 |
| rabbit | Waffles | 1 |

## 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.
