# How to rank rows within each group in SQL

Source: https://sqlstreak.com/sql/how-to/rank-within-group · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


**Short answer:** Add `RANK() OVER (PARTITION BY category ORDER BY price DESC)` as a column. PARTITION BY restarts the ranking for each group and every row stays in the result; `PERCENT_RANK()` gives the position as a fraction from 0 to 1.

**The task:** Show every row with its position inside its own group, such as each item's price rank within its category.

### Each item's price rank in its category

```sql
SELECT category, name, price,
       RANK() OVER (PARTITION BY category ORDER BY price DESC) AS rank_in_category
FROM menu_items
ORDER BY category, rank_in_category;
```

Result (both engines):

| category | name | price | rank_in_category |
| --- | --- | --- | --- |
| dessert | Tiramisu | 6.5 | 1 |
| drink | Lemon Soda | 3 | 1 |
| drink | Cola | 2.5 | 2 |
| pizza | BBQ Meteor | 15.5 | 1 |
| pizza | Four Cheese Orbit | 14 | 2 |
| pizza | Pepperoni Rocket | 13.5 | 3 |
| pizza | Veggie Nebula | 13 | 4 |
| pizza | Margherita | 11 | 5 |

(8 of 11 rows)

### Rank in the group next to rank overall

One window without PARTITION BY, one with.

```sql
SELECT name, species, age,
       RANK() OVER (ORDER BY age DESC) AS oldest_overall,
       RANK() OVER (PARTITION BY species ORDER BY age DESC) AS oldest_in_species
FROM pets
ORDER BY oldest_overall, name
LIMIT 8;
```

Result (both engines):

| name | species | age | oldest_overall | oldest_in_species |
| --- | --- | --- | --- | --- |
| Tofu | cat | 11 | 1 | 1 |
| Gizmo | dog | 10 | 2 | 1 |
| Bruno | dog | 9 | 3 | 2 |
| Socks | cat | 8 | 4 | 2 |
| Pickles | dog | 7 | 5 | 3 |
| Ziggy | bird | 6 | 6 | 1 |
| Luna | cat | 5 | 7 | 3 |
| Rocket | dog | 5 | 7 | 4 |

### Position as a fraction

0 for the lowest in the group, 1 for the highest.

```sql
SELECT species, name, weight_kg,
       ROUND(CAST(PERCENT_RANK() OVER (PARTITION BY species ORDER BY weight_kg) AS NUMERIC), 2) AS weight_pct_rank
FROM pets
WHERE species IN ('dog', 'cat')
ORDER BY species, weight_kg;
```

Result (both engines):

| species | name | weight_kg | weight_pct_rank |
| --- | --- | --- | --- |
| cat | Pepper | 3.2 | 0 |
| cat | Luna | 3.8 | 0.17 |
| cat | Olive | 3.9 | 0.33 |
| cat | Mochi | 4.1 | 0.5 |
| cat | Tofu | 4.6 | 0.67 |
| cat | Pumpkin | 5.2 | 0.83 |
| cat | Socks | 7.4 | 1 |
| dog | Bean | 6 | 0 |

(8 of 15 rows)

## How it works

- `PARTITION BY category`: Each category is ranked on its own.
- `ORDER BY price DESC`: Most expensive first within each category.
- `RANK()`: Ties would share a rank; every row is kept, nothing is filtered.

## Variations

### Keep only the top of each group

Wrap it and filter on the rank: see top N per group.

## Common mistakes

- Forgetting PARTITION BY: you get one ranking over the whole table.
- Expecting the final ORDER BY to set the ranking: the ORDER BY inside OVER does that.
- Ranking by a column with NULLs: in PostgreSQL NULLs sort as the largest values, in SQLite as the smallest. Add NULLS LAST to be explicit.
- `ROUND(PERCENT_RANK() OVER (…), 2)` fails in PostgreSQL: PERCENT_RANK returns double precision, and ROUND(x, 2) needs numeric. `CAST(… AS NUMERIC)` first works in both.

## PostgreSQL and SQLite

Ranking works the same in both. They differ only in where NULLs sort by default (last in PostgreSQL ascending, first in SQLite), which `NULLS FIRST` / `NULLS LAST` settles in both.
