SQL how-to
How to rank rows within each group in SQL
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
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
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 (PostgreSQL and SQLite, identical): 11 rows
| 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 |
| side | Wings | 8.5 | 1 |
| side | Caesar Salad | 7 | 2 |
| side | Garlic Knots | 5 | 3 |
Try it: PostgreSQL playground · SQLite playground
How it works
- 1
PARTITION BY categoryEach category is ranked on its own.
- 2
ORDER BY price DESCMost expensive first within each category.
- 3
RANK()Ties would share a rank; every row is kept, nothing is filtered.
More examples
Rank in the group next to rank overall
One window without PARTITION BY, one with.
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 (PostgreSQL and SQLite, identical): 8 rows
| 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 |
Try it: PostgreSQL playground · SQLite playground
Position as a fraction
0 for the lowest in the group, 1 for the highest.
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 (PostgreSQL and SQLite, identical): first 12 of 15 rows
| 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 |
| dog | Noodle | 8.9 | 0.14 |
| dog | Gizmo | 9.1 | 0.29 |
| dog | Biscuit | 11.2 | 0.43 |
| dog | Rocket | 19.8 | 0.57 |
Try it: PostgreSQL playground · SQLite playground
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.
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.