sqlstreak

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

categorynamepricerank_in_category
dessertTiramisu6.51
drinkLemon Soda31
drinkCola2.52
pizzaBBQ Meteor15.51
pizzaFour Cheese Orbit142
pizzaPepperoni Rocket13.53
pizzaVeggie Nebula134
pizzaMargherita115
sideWings8.51
sideCaesar Salad72
sideGarlic Knots53

Try it: PostgreSQL playground · SQLite playground

How it works

  1. 1
    PARTITION BY category

    Each category is ranked on its own.

  2. 2
    ORDER BY price DESC

    Most expensive first within each category.

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

namespeciesageoldest_overalloldest_in_species
Tofucat1111
Gizmodog1021
Brunodog932
Sockscat842
Picklesdog753
Ziggybird661
Lunacat573
Rocketdog574

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

speciesnameweight_kgweight_pct_rank
catPepper3.20
catLuna3.80.17
catOlive3.90.33
catMochi4.10.5
catTofu4.60.67
catPumpkin5.20.83
catSocks7.41
dogBean60
dogNoodle8.90.14
dogGizmo9.10.29
dogBiscuit11.20.43
dogRocket19.80.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

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