Sub topic 5
Ranking Rows: ROW_NUMBER, RANK and DENSE_RANK
This chapter assumes you know SELECT, ORDER BY, GROUP BY, aggregate functions, subqueries, and common table expressions (CTEs). It also assumes your database supports window functions.
Ranking without collapsing rows
A window function calculates a value for each input row while keeping every row in the result. Ranking functions use an OVER clause to define which rows to consider and how to order them.
Suppose sales contains these columns:
salesperson: the person who made the saleregion: the salesperson's regionamount: the sale amount
A normal aggregate query can find the largest sale:
SELECT MAX(amount) AS largest_sale
FROM sales;However, this query returns only the amount. It does not return every salesperson with that amount. A ranking window function keeps the original rows and adds a rank to each one.
Choosing a ranking function
The three common ranking functions handle ties differently:
| Function | Tied rows receive | Numbers after a tie |
|---|---|---|
| `ROW_NUMBER()` | Different numbers | Never skipped |
| `RANK()` | The same number | Skipped |
| `DENSE_RANK()` | The same number | Not skipped |
For example, if the amounts are 500, 400, 400, and 250:
ROW_NUMBER()returns 1, 2, 3, and 4.RANK()returns 1, 2, 2, and 4.DENSE_RANK()returns 1, 2, 2, and 3.
SELECT
salesperson,
amount,
ROW_NUMBER() OVER (ORDER BY amount DESC) AS row_num,
RANK() OVER (ORDER BY amount DESC) AS rnk,
DENSE_RANK() OVER (ORDER BY amount DESC) AS dense_rnk
FROM sales;Use ROW_NUMBER() when every row needs a unique position, such as selecting exactly three rows. Use RANK() when competition-style ranking matters and a tie should create a gap. Use DENSE_RANK() when equal values share a position but positions should remain consecutive.
Ranking within each group
Add PARTITION BY when ranking should restart for each group. A partition is the subset of rows sharing the same value, such as one region.
SELECT
region,
salesperson,
amount,
ROW_NUMBER() OVER (
PARTITION BY region
ORDER BY amount DESC
) AS regional_row_num,
RANK() OVER (
PARTITION BY region
ORDER BY amount DESC
) AS regional_rank
FROM sales;The query ranks sales separately in each region. PARTITION BY does not combine rows. It only divides the window into independent groups.
Filtering ranked results
Window functions are evaluated after WHERE, so this is invalid in many SQL systems:
SELECT salesperson, amount
FROM sales
WHERE ROW_NUMBER() OVER (ORDER BY amount DESC) <= 3;Compute the rank in a CTE, then filter it in the outer query:
WITH ranked AS (
SELECT
region,
salesperson,
amount,
ROW_NUMBER() OVER (
PARTITION BY region
ORDER BY amount DESC, salesperson
) AS rn
FROM sales
)
SELECT region, salesperson, amount
FROM ranked
WHERE rn <= 3
ORDER BY region, rn;The second ordering expression, salesperson, makes ties deterministic for ROW_NUMBER(). Without it, the database may choose either tied row first.
Exercise
Which function should you use to return every sale tied for third place in each region, including possible ties?
Show answer
Use RANK() or DENSE_RANK(), then filter for rank <= 3. Use RANK() when skipped positions should reflect the number of tied rows.