Ranking Window Functions

OVER() with RANK()

MySQL ranking window functions keep every result row and add ranking information. They use an OVER() clause to define ordering and optional partitions.

RANK() OVER (ORDER BY expression)
Using student sample table.
SELECT id, name, class, mark, gender,
       RANK() OVER (ORDER BY mark) AS rank_no
FROM student
WHERE class = 'Four';
id name class mark gender r1
6Alex JohnFour55male1
10Big JohnFour55female1
4Krish StarFour60female3
5John MikeFour60female3
21Babby JohnFour69female5
1John DeoFour75female6
15Tade RowFour88male7
16GimmyFour88male7
31 Marry ToeeyFour88male7
RANK() leaves gaps after ties. In this result, tied marks share the same rank and the next rank skips ahead.

DENSE_RANK()

When rows tie on the ordering value, RANK() leaves gaps after the tie, while DENSE_RANK() does not.
We will use DENSE_RANK with RANK and ROW_NUMBER for comparison.
SELECT id, name, class, mark, gender,
       ROW_NUMBER() OVER (ORDER BY mark, id) AS row_no,
       RANK() OVER (ORDER BY mark) AS rank_no,
       DENSE_RANK() OVER (ORDER BY mark) AS dense_rank_no
FROM student
WHERE class = 'Four';
id name class markgenderrn r1 DR
6Alex JohnFour55male111
10Big JohnFour55female211
4Krish StarFour60female332
5John MikeFour60female432
21Babby JohnFour69female553
1John DeoFour75female664
15Tade RowFour88male775
16GimmyFour88male875
31Marry ToeeyFour88male975
In case of DENSE_RANK no rank is skipped in case of tie over rank. If there is no duplicate value ( mark ) then there is no difference between RANK() and DENSE_RANK()

ROW_NUMBER()

ROW_NUMBER() assigns a unique sequential number starting at 1. For deterministic numbering of tied marks, include a stable tie-breaker such as id in the window ORDER BY clause.
SELECT id, name, class, mark, gender,
       ROW_NUMBER() OVER (ORDER BY mark, id) AS row_no
FROM student
WHERE class = 'Four';
Output
idnameclassmarkgenderrn
6 Alex JohnFour55male1
10Big JohnFour55female2
4Krish StarFour60female3
5John MikeFour60female4
21Babby JohnFour69female5
1John DeoFour75female6
15Tade RowFour88male7
16GimmyFour88male8
31Marry ToeeyFour88male9
Using Partition
SELECT id, name, class, mark, gender,
       ROW_NUMBER() OVER (PARTITION BY gender ORDER BY mark, id) AS row_no
FROM student
WHERE class = 'Four';
Output
idnameclassmarkgenderrn
10 Big JohnFour55female1
4Krish StarFour60female2
5John MikeFour60female3
21Babby JohnFour69female4
1John DeoFour75female5
6Alex JohnFour55male1
15Tade RowFour88male2
16GimmyFour88male3
31Marry ToeeyFour88male4

NTILE(n)

We can group the rows (buckets) by using NTILE(n). Here n is a positive integer.
SELECT id, name, class, mark, gender,
       NTILE(3) OVER (ORDER BY mark, id) AS tile_no
FROM student
WHERE class = 'Four';
idnameclassmarkgendermy_NTILE
6Alex JohnFour55male1
10Big JohnFour55female1
4Krish StarFour60female1
5John MikeFour60female2
21Babby JohnFour69female2
1John DeoFour75female2
15Tade RowFour88male3
16GimmyFour88male3
31Marry ToeeyFour88male3

Over & partition window LEAD() LAG() Sum Multiple column


Subscribe to our YouTube Channel here



plus2net.com




SQL Video Tutorials










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer