DENSE_RANK
Rank rows without gaps: ties share a rank, and the next rank is always the next integer (1, 2, 2, 3). Use DENSE_RANK when you care which bucket a row falls into (gold/silver/bronze tiers, top-N groups) rather than its unique competitive position.
Syntax #
sql
SELECT key_col, metric,
DENSE_RANK() OVER (PARTITION BY key_col ORDER BY metric DESC) AS dense_rank
FROM table_name;Example #
Loading SQL editor...
Tip
"Top 3 per group" usually wants DENSE_RANK, not RANK or ROW_NUMBER. WHERE dense_rank <= 3 keeps everyone in the top three tiers (including ties), which is usually what stakeholders mean by "top 3" — not "the first three rows alphabetically."