RANK
Rank rows by an ordered metric. Ties share a rank, and the next rank skips the count of ties (1, 2, 2, 4). Use RANK when "tied for 2nd, nobody was 3rd" is the narrative you want, like leaderboards.
Syntax #
sql
SELECT key_col, metric,
RANK() OVER (PARTITION BY key_col ORDER BY metric DESC) AS rnk
FROM table_name;Example #
Loading SQL editor...
Info
Leaderboards want RANK; tiered bucketing wants DENSE_RANK; unique numbering wants ROW_NUMBER. A "Top 10 customers" list built with RANK and LIMIT 10 can silently cut a tied group in half: if three customers share rank 9, you get 9 of them and drop the fourth. Decide deliberately: ROW_NUMBER when you need exactly 10, DENSE_RANK when you want the top three tiers (and all their members).