MySQL | Ranking Functions

20 May, 2019
The ranking functions in MySql are used to rank each row of a partition. The ranking functions are also part of MySQL windows functions list.
These functions are always used with OVER() clause.
The ranking functions always assign rank on basis of ORDER BY clause.
The rank is assigned to rows in a sequential manner.
The assignment of rank to rows always start with 1 for every new partition.
There are 3 types of ranking functions supported in MySQL-
dense_rank():This function will assign rank to each row within a partition without gaps. Basically, the ranks are assigned in a consecutive manner i.e if there is a tie between values then they will be assigned the same rank, and next rank value will be one greater then the previous rank assigned.rank():This function will assign rank to each row within a partition with gaps. Here, ranks are assigned in a non-consecutive manner i.e if there is a tie between values then they will be assigned same rank, and next rank value will be previous rank + no of peers(duplicates).percent_rank():It returns the percentile rank of a row within a partition that ranges from 0 to 1. It tells the percentage of partition values less than the value in the current row, excluding the highest value.
dense_rank():This function will assign rank to each row within a partition without gaps. Basically, the ranks are assigned in a consecutive manner i.e if there is a tie between values then they will be assigned the same rank, and next rank value will be one greater then the previous rank assigned.
rank():This function will assign rank to each row within a partition with gaps. Here, ranks are assigned in a non-consecutive manner i.e if there is a tie between values then they will be assigned same rank, and next rank value will be previous rank + no of peers(duplicates).
percent_rank():It returns the percentile rank of a row within a partition that ranges from 0 to 1. It tells the percentage of partition values less than the value in the current row, excluding the highest value.
In order to understand these functions in a better way.Let consider a table “result”–
Queries:
dense_rank() function-SELECT subjects, s_name, mark, dense_rank() 
OVER ( partition by subjects order by mark desc ) 
AS 'dense_rank' FROM result;
Output-SubjectsNameMarkDense_rankEnglishSwarna1001EnglishAnkita902EnglishPratibha703MathsPratibha1001MathsSwarna852MathsAnkita653ScienceAnkita801SciencePratibha801ScienceSwarna502Explanation-Here, table is partitioned on the basis of “subjects”.order by clause is used to arrange rows of each partition in descending order by “mark”.dense_rank() is used to rank students in each subject.Note, for science subject there is a tie between Ankita and Pratibha, so they both are assigned same rank. The next rank value is incremented by 1 i.e 2 for Swarna.rank() function-SELECT subjects, s_name, mark, rank() 
OVER ( partition by subjects order by mark desc ) 
AS 'rank' FROM result;
Output-SubjectsNameMarkrankEnglishSwarna1001EnglishAnkita902EnglishPratibha703MathsPratibha1001MathsSwarna852MathsAnkita653ScienceAnkita801SciencePratibha801ScienceSwarna503Explanation-It’s output is similar to dense_rank() function.Except, that for Science subject in case of a tie between Ankita and Pratibha, the next rank value is incremented by 2 i.e 3 for Swarna.percent_rank() function-SELECT subjects, s_name, mark, percent_rank() 
OVER ( partition by subjects order by mark ) 
AS 'percent_rank' FROM result;
Output-SubjectsNameMarkpercent_rankEnglishPratibha700EnglishAnkita900.5EnglishSwarna1001MathsAnkita650MathsSwarna850.5MathsPratibha1001ScienceSwarna500SciencePratibha800.5ScienceAnkita800.5Explanation:Here, the percent_rank() function calculate percentile rank in ascending order by “mark” column.percent_rank is calculated using following formula-(rank - 1) / (rows - 1)
rank is the rank of each row of the partition resulted using rank() function.rows represent the no of rows in that partition.To, clear this formula consider following query-SELECT subjects, s_name, mark, rank() 
OVER ( partition by subjects order by mark )-1 
AS 'rank-1', count(*) over (partition by subjects)-1
AS 'total_rows-1', percent_rank()
OVER ( partition by subjects order by mark ) AS 'percenr_rank'
FROM result;
Output-SubjectsNameMarkrank-1total_rows-1percent_rankEnglishPratibha70020EnglishAnkita90120.5EnglishSwarna100221MathsAnkita65020MathsSwarna85120.5MathsPratibha100221ScienceSwarna50020ScienceAnkita80120.5SciencePratibha80120.5Note: While using ranking function, in MySQL query the use of order by clause is must otherwise all rows are considered as peers i.e(duplicates) and all rows are assigned same rank i.e 1.My Personal Notes
arrow_drop_upSave
dense_rank() function-SELECT subjects, s_name, mark, dense_rank() 
OVER ( partition by subjects order by mark desc ) 
AS 'dense_rank' FROM result;
Output-SubjectsNameMarkDense_rankEnglishSwarna1001EnglishAnkita902EnglishPratibha703MathsPratibha1001MathsSwarna852MathsAnkita653ScienceAnkita801SciencePratibha801ScienceSwarna502Explanation-Here, table is partitioned on the basis of “subjects”.order by clause is used to arrange rows of each partition in descending order by “mark”.dense_rank() is used to rank students in each subject.Note, for science subject there is a tie between Ankita and Pratibha, so they both are assigned same rank. The next rank value is incremented by 1 i.e 2 for Swarna.
SELECT subjects, s_name, mark, dense_rank() 
OVER ( partition by subjects order by mark desc ) 
AS 'dense_rank' FROM result;

Output-
Explanation-
Here, table is partitioned on the basis of “subjects”.
order by clause is used to arrange rows of each partition in descending order by “mark”.
dense_rank() is used to rank students in each subject.
Note, for science subject there is a tie between Ankita and Pratibha, so they both are assigned same rank. The next rank value is incremented by 1 i.e 2 for Swarna.
rank() function-SELECT subjects, s_name, mark, rank() 
OVER ( partition by subjects order by mark desc ) 
AS 'rank' FROM result;
Output-SubjectsNameMarkrankEnglishSwarna1001EnglishAnkita902EnglishPratibha703MathsPratibha1001MathsSwarna852MathsAnkita653ScienceAnkita801SciencePratibha801ScienceSwarna503Explanation-It’s output is similar to dense_rank() function.Except, that for Science subject in case of a tie between Ankita and Pratibha, the next rank value is incremented by 2 i.e 3 for Swarna.
SELECT subjects, s_name, mark, rank() 
OVER ( partition by subjects order by mark desc ) 
AS 'rank' FROM result;

Output-
Explanation-
It’s output is similar to dense_rank() function.
Except, that for Science subject in case of a tie between Ankita and Pratibha, the next rank value is incremented by 2 i.e 3 for Swarna.
percent_rank() function-SELECT subjects, s_name, mark, percent_rank() 
OVER ( partition by subjects order by mark ) 
AS 'percent_rank' FROM result;
Output-SubjectsNameMarkpercent_rankEnglishPratibha700EnglishAnkita900.5EnglishSwarna1001MathsAnkita650MathsSwarna850.5MathsPratibha1001ScienceSwarna500SciencePratibha800.5ScienceAnkita800.5Explanation:Here, the percent_rank() function calculate percentile rank in ascending order by “mark” column.percent_rank is calculated using following formula-(rank - 1) / (rows - 1)
rank is the rank of each row of the partition resulted using rank() function.rows represent the no of rows in that partition.To, clear this formula consider following query-SELECT subjects, s_name, mark, rank() 
OVER ( partition by subjects order by mark )-1 
AS 'rank-1', count(*) over (partition by subjects)-1
AS 'total_rows-1', percent_rank()
OVER ( partition by subjects order by mark ) AS 'percenr_rank'
FROM result;
Output-SubjectsNameMarkrank-1total_rows-1percent_rankEnglishPratibha70020EnglishAnkita90120.5EnglishSwarna100221MathsAnkita65020MathsSwarna85120.5MathsPratibha100221ScienceSwarna50020ScienceAnkita80120.5SciencePratibha80120.5
SELECT subjects, s_name, mark, percent_rank() 
OVER ( partition by subjects order by mark ) 
AS 'percent_rank' FROM result;

Output-
Explanation:
Here, the percent_rank() function calculate percentile rank in ascending order by “mark” column.
percent_rank is calculated using following formula-
(rank - 1) / (rows - 1)

rank is the rank of each row of the partition resulted using rank() function.
rows represent the no of rows in that partition.
To, clear this formula consider following query-
SELECT subjects, s_name, mark, rank() 
OVER ( partition by subjects order by mark )-1 
AS 'rank-1', count(*) over (partition by subjects)-1
AS 'total_rows-1', percent_rank()
OVER ( partition by subjects order by mark ) AS 'percenr_rank'
FROM result;

Output-
Note: While using ranking function, in MySQL query the use of order by clause is must otherwise all rows are considered as peers i.e(duplicates) and all rows are assigned same rank i.e 1.
ManasChhabra2
mysql
DBMS
SQL
DBMS
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
CTE in SQL
Introduction of DBMS (Database Management System) | Set 1
Difference between Clustered and Non-clustered index
SQL Trigger | Student Database
Introduction of B-Tree
SQL | DDL, DQL, DML, DCL and TCL Commands
How to find Nth highest salary from a table
CTE in SQL
SQL | ALTER (RENAME)
SQL Trigger | Student Database