MySQL | Ranking Functions
Last Updated :
02 Feb, 2023
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 than 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"-
s_name | subjects | mark |
---|
Pratibha | Maths | 100 |
Ankita | Science | 80 |
Swarna | English | 100 |
Ankita | Maths | 65 |
Pratibha | Science | 80 |
Swarna | Science | 50 |
Pratibha | English | 70 |
Swarna | Maths | 85 |
Ankita | English | 90 |
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-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.
Subjects | Name | Mark | Dense_rank |
---|
English | Swarna | 100 | 1 |
English | Ankita | 90 | 2 |
English | Pratibha | 70 | 3 |
Maths | Pratibha | 100 | 1 |
Maths | Swarna | 85 | 2 |
Maths | Ankita | 65 | 3 |
Science | Ankita | 80 | 1 |
Science | Pratibha | 80 | 1 |
Science | Swarna | 50 | 2 |
- rank() function-
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.
Subjects | Name | Mark | rank |
---|
English | Swarna | 100 | 1 |
English | Ankita | 90 | 2 |
English | Pratibha | 70 | 3 |
Maths | Pratibha | 100 | 1 |
Maths | Swarna | 85 | 2 |
Maths | Ankita | 65 | 3 |
Science | Ankita | 80 | 1 |
Science | Pratibha | 80 | 1 |
Science | Swarna | 50 | 3 |
- percent_rank() function-
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-
Subjects | Name | Mark | rank-1 | total_rows-1 | percent_rank |
---|
English | Pratibha | 70 | 0 | 2 | 0 |
English | Ankita | 90 | 1 | 2 | 0.5 |
English | Swarna | 100 | 2 | 2 | 1 |
Maths | Ankita | 65 | 0 | 2 | 0 |
Maths | Swarna | 85 | 1 | 2 | 0.5 |
Maths | Pratibha | 100 | 2 | 2 | 1 |
Science | Swarna | 50 | 0 | 2 | 0 |
Science | Ankita | 80 | 1 | 2 | 0.5 |
Science | Pratibha | 80 | 1 | 2 | 0.5 |
Subjects | Name | Mark | percent_rank |
---|
English | Pratibha | 70 | 0 |
English | Ankita | 90 | 0.5 |
English | Swarna | 100 | 1 |
Maths | Ankita | 65 | 0 |
Maths | Swarna | 85 | 0.5 |
Maths | Pratibha | 100 | 1 |
Science | Swarna | 50 | 0 |
Science | Pratibha | 80 | 0.5 |
Science | Ankita | 80 | 0.5 |
Similar Reads
RIGHT() Function in MySQL
RIGHT() function in MySQL is used to extract a specified number of characters from the right side of a given string. Second argument is used to decide, how many characters it should return. Syntax : RIGHT( str, len ) Parameter : This function accepts two parameter as mentioned above and described be
3 min read
ROUND() Function in MySQL
The ROUND() function in MySQL is used to round a number to a specified number of decimal places. If no specified number of decimal places is provided for round-off, it rounds off the number to the nearest integer. Syntax ROUND(X, D) Parameter Explanation This method accepts two parameters in the syn
2 min read
MySQL QUARTER() Function
MySQL QUARTER() function is used to return the quarter of the year for a given date value. QUARTER function in MySQL returns values between 1 to 4. For January to March, it returns 1, for April to June, it returns 2, For July to September, it returns 3, For October to December, it returns 4. SyntaxM
2 min read
SQRT() Function in MySQL
The SQRT() function in MySQL calculates the square root of a non-negative number, returning NULL for negative inputs. It is a built-in function that provides high precision and is optimized for performance and making it ideal for mathematical and scientific applications.In the article, we will cover
3 min read
Window Functions in SQL
SQL window functions are essential for advanced data analysis and database management. They enable calculations across a specific set of rows, known as a "window," while retaining the individual rows in the dataset. Unlike traditional aggregate functions that summarize data for the entire group, win
7 min read
MySQL IF( ) Function
The MySQL IF() function is a control flow function that returns different values based on the result of a condition. IF() Function in MySQLThe IF() function in MySQL returns a value if the condition is TRUE and another value if the condition is FALSE. The MySQL IF() function can return values that c
2 min read
POSITION() function in MySQL
POSITION() : This function in MySQL is used for finding the location of a substring in a string. It will return the location of the first occurrence of the substring in the string. If the substring is not present in the string then it will return 0. When searching for the location of a substring in
2 min read
STRCMP() Function in MySQL
STRCMP() function in MySQL is used to compare two strings. If both of the strings are same then it returns 0, if the first argument is smaller than the second according to the defined order it returns -1 and it returns 1 when the second one is smaller the first one. Syntax : STRCMP(Str1, Str2) Param
3 min read
SECOND() Function in MySQL
SECOND() function in MySQL is used to return the second portion of a specified time or date-time value. The first parameter in this function will be the date/Date Time. This function returns the seconds from the given date value. The return value (seconds) will be in the range of 0 to 59. In this fu
2 min read
SUM() Function in MySQL
The SUM() function in MySQL is a powerful aggregate function used to calculate the total sum of values in a numeric column. By summing up the values in the specified column, this function helps in generating overall totals and performing calculations that provide meaningful insights from our data. I
4 min read