Blog · Guide

SQL Window Functions: RANK Without Losing Rows

RANK() OVER (ORDER BY …) adds a ranking column while keeping every product row — unlike GROUP BY.

products Milk $3 Cheese $8 RANK() still 2 rows Cheese · rank 1 Milk · rank 2
Unlike GROUP BY, window functions keep every row and add a calculated column.

Ranking that keeps detail

SELECT name, price,
  RANK() OVER (ORDER BY price DESC) AS price_rank
FROM products;

You still see each product. GROUP BY would collapse them. Use windows when you need both the row and a running comparison.

PARTITION BY

Want ranks inside each category? RANK() OVER (PARTITION BY category ORDER BY price DESC) restarts the rank per group — practice it on the window partition drill.

Try it yourself

The fastest way to lock this in is a short drill on the same Northstar Market tables.

Practice RANK()

Related guides