Ranking Functions
Ranking functions in SQL are window functions that give each row a number based on its position in an ordered window. They are used to find the top payments, the latest payment per student, the first sign-up among duplicates, or which quarter a value falls in. SQL has four: ROW_NUMBER, RANK, DENSE_RANK and NTILE. They differ only in how they treat ties, and the Fee Tracker has exactly the ties needed to see the difference: two payments of $250.00 and two of $225.00. In this lesson, we will compare all four, then use them for "top N per group" and for removing duplicates.
In One Minute
ROW_NUMBER()gives 1, 2, 3, ... with no ties: tied rows get different numbers.RANK()gives tied rows the same number, then skips: 1, 2, 3, 3, 5.DENSE_RANK()gives tied rows the same number without skipping: 1, 2, 3, 3, 4.NTILE(n)splits the rows intonnearly equal buckets.- All need
OVER (ORDER BY ...); addPARTITION BYto restart the numbering for each group.
Syntax
ROW_NUMBER() OVER ([PARTITION BY ...] ORDER BY ...)
RANK() OVER ([PARTITION BY ...] ORDER BY ...)
DENSE_RANK() OVER ([PARTITION BY ...] ORDER BY ...)
NTILE(n) OVER ([PARTITION BY ...] ORDER BY ...)The ORDER BY inside OVER decides what "first" means. It is separate from the query's final ORDER BY.
ROW_NUMBER, RANK and DENSE_RANK Side by Side
The following query ranks the payments by amount, largest first, with all three functions:
SELECT payment_id, amount_usd,
ROW_NUMBER() OVER (ORDER BY amount_usd DESC, payment_id) AS row_number,
RANK() OVER (ORDER BY amount_usd DESC) AS rank,
DENSE_RANK() OVER (ORDER BY amount_usd DESC) AS dense_rank
FROM payments
ORDER BY amount_usd DESC, payment_id; payment_id | amount_usd | row_number | rank | dense_rank
------------+------------+------------+------+------------
4 | 550.00 | 1 | 1 | 1
1 | 450.00 | 2 | 2 | 2
2 | 250.00 | 3 | 3 | 3
3 | 250.00 | 4 | 3 | 3
6 | 225.00 | 5 | 5 | 4
7 | 225.00 | 6 | 5 | 4
5 | 200.00 | 7 | 7 | 5
(7 rows)Explanation: Payments 2 and 3 are both $250.00:
ROW_NUMBERgives them 3 and 4. It never ties, so we addpayment_idto itsORDER BYto decide which tied row comes first; otherwise the choice is arbitrary.RANKgives both 3, then skips to 5, because two rows used up places 3 and 4. It works like a race: two runners tie for third, and the next is fifth.DENSE_RANKgives both 3, then continues with 4, with no gaps.
| Function | 550, 450, 250, 250, 225, 225, 200 | Ties | Gaps |
|---|---|---|---|
ROW_NUMBER() | 1, 2, 3, 4, 5, 6, 7 | Broken | None |
RANK() | 1, 2, 3, 3, 5, 5, 7 | Kept | After ties |
DENSE_RANK() | 1, 2, 3, 3, 4, 4, 5 | Kept | None |
NTILE: Buckets
NTILE(n) divides the ordered rows into n buckets of nearly equal size, numbered 1 to n:
SELECT payment_id, amount_usd,
NTILE(3) OVER (ORDER BY amount_usd DESC, payment_id) AS third
FROM payments
ORDER BY amount_usd DESC, payment_id; payment_id | amount_usd | third
------------+------------+-------
4 | 550.00 | 1
1 | 450.00 | 1
2 | 250.00 | 1
3 | 250.00 | 2
6 | 225.00 | 2
7 | 225.00 | 3
5 | 200.00 | 3
(7 rows)Explanation: Seven rows don't divide evenly by three, so the first bucket gets the extra row: 3, 2 and 2 rows. Note that NTILE splits by position, not by value: the two $250.00 payments land in different buckets.
Top N per Group
Rank inside each group with PARTITION BY, then keep the rows ranked <= N. Each student's latest payment:
WITH ranked AS (
SELECT student_id, payment_id, paid_on,
ROW_NUMBER() OVER (PARTITION BY student_id ORDER BY paid_on DESC) AS rn
FROM payments
)
SELECT student_id, payment_id, paid_on
FROM ranked
WHERE rn = 1
ORDER BY student_id; student_id | payment_id | paid_on
------------+------------+------------
101 | 5 | 2026-01-05
103 | 7 | 2025-12-19
(2 rows)Explanation: The numbering restarts at 1 for each student, counting from the latest payment. Keeping rn = 1 leaves one row per student. This is the portable version of PostgreSQL's DISTINCT ON from Correlated Subqueries.
Use RANK instead when ties should all be kept. Each student's two largest payments:
WITH ranked AS (
SELECT student_id, payment_id, amount_usd,
RANK() OVER (PARTITION BY student_id ORDER BY amount_usd DESC) AS rnk
FROM payments
)
SELECT student_id, payment_id, amount_usd, rnk
FROM ranked
WHERE rnk <= 2
ORDER BY student_id, rnk, payment_id; student_id | payment_id | amount_usd | rnk
------------+------------+------------+-----
101 | 4 | 550.00 | 1
101 | 1 | 450.00 | 2
103 | 6 | 225.00 | 1
103 | 7 | 225.00 | 1
(4 rows)Explanation: Srihitha's top two are $550.00 and $450.00. Navika's two payments are both $225.00, so both rank 1, and both are kept. With ROW_NUMBER, one of them would be ranked 2, and the result would look as if one payment were larger.
Removing Duplicates with ROW_NUMBER
ROW_NUMBER finds the extra copies of duplicate rows: number the rows within each set of duplicates, then keep (or delete) everything after the first. A sign-up form saved Navika three times:
CREATE TABLE signups (student_id INTEGER, email TEXT, signed_up_on DATE);
INSERT INTO signups VALUES
(103, 'navika@example.com', '2026-01-02'),
(103, 'navika@example.com', '2026-01-04'),
(102, 'advitha@example.com', '2026-01-03'),
(103, 'navika@example.com', '2026-01-05');
WITH numbered AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY student_id ORDER BY signed_up_on) AS rn
FROM signups
)
SELECT student_id, email, signed_up_on
FROM numbered
WHERE rn = 1
ORDER BY student_id;CREATE TABLE
INSERT 0 4
student_id | email | signed_up_on
------------+---------------------+--------------
102 | advitha@example.com | 2026-01-03
103 | navika@example.com | 2026-01-02
(2 rows)Explanation: Each student's sign-ups are numbered from the earliest. Keeping rn = 1 keeps one row per student, the first. To delete the extra rows from the table instead, delete the rows whose rn > 1, using the table's primary key to identify them.
Common Mistakes
ROW_NUMBER without a tie-breaker. Tied rows get numbers in an arbitrary order. Add a unique column to the ORDER BY.
Using ROW_NUMBER when ties matter. It hides them. Use RANK or DENSE_RANK.
Filtering on a rank in WHERE. Rank in a CTE, filter outside.
Confusing the two ORDER BYs. The one in OVER decides the ranking; the final one decides the display order.
Interview Corner
What's the difference between ROW_NUMBER, RANK and DENSE_RANK?
ROW_NUMBER gives unique numbers, breaking ties arbitrarily. RANK gives ties the same number and skips the following numbers. DENSE_RANK gives ties the same number without skipping.
How do you get the top 3 rows per group?
Compute ROW_NUMBER() (or RANK() to keep ties) OVER (PARTITION BY group ORDER BY value DESC) in a CTE, then select the rows where the rank is <= 3.
How do you remove duplicate rows?
Number them with ROW_NUMBER() OVER (PARTITION BY the columns that define a duplicate ORDER BY ...), and keep or delete by rn = 1 versus rn > 1.
Practice
Challenge: Rank the courses by fee within each technology, highest first, using RANK. Show technology, title, fee and the rank, sorted by technology and rank.
Show solution
SELECT technology, title, fee_usd,
RANK() OVER (PARTITION BY technology ORDER BY fee_usd DESC) AS rank_in_technology
FROM courses
ORDER BY technology, rank_in_technology; technology | title | fee_usd | rank_in_technology
------------+-------------------+---------+--------------------
Git | Git Basics | 250.00 | 1
Java | Java Collections | 550.00 | 1
Java | Java OOP | 500.00 | 2
Java | Java Basics | 450.00 | 3
JavaScript | JavaScript Basics | 450.00 | 1
SQL | SQL Basics | 400.00 | 1
(6 rows)Within Java, Collections ranks 1, OOP 2 and Basics 3. Every other technology has one course, so each ranks 1 in its own partition.
Key Takeaways
| You want to | Write |
|---|---|
| Unique numbers | ROW_NUMBER() OVER (ORDER BY amount_usd DESC, payment_id) |
| Ranks with gaps | RANK() OVER (ORDER BY amount_usd DESC) |
| Ranks without gaps | DENSE_RANK() OVER (ORDER BY amount_usd DESC) |
| Latest per student | ROW_NUMBER() OVER (PARTITION BY student_id ORDER BY paid_on DESC), then rn = 1 |
Was this tutorial helpful?