Javatpoint Logo
Javatpoint Logo

MySQL Ranking Functions

MySQL uses a ranking function that allows us to rank each row of a partition in the databases. The ranking functions are also a sub-part of a window function in MySQL. The ranking functions in MySQL can be used with the following clauses:

  • They always work with the OVER()
  • They assign a rank to each row based on the ORDER BY
  • They assign a rank to each row in the sequential order.
  • They always assign a rank to rows, starting with one for each new partition.

NOTE: It is to be noted that MySQL provides support for the ranking and window functions since version 8.0.

MySQL supports the following three types of ranking functions:

  1. Dense Rank
  2. Rank
  3. Percent Rank

Now, we are going to discuss each ranking functions in detail:

MySQL dense_rank()

It is a function that assigns a rank for every row within a partition or result set without any gaps. The rank of rows is always assigned in consecutive order (increased by one from the previous row). Sometimes you will get a tie between the values, then the dense_rank will assign it with the same rank, and its next rank will be its next consecutive number.

The following are the syntax of dense_rank():

In the above syntax, the PARTITION BY clause partition the result set return by FROM clause, and then the dense_rank function applied on each partition. Next, the ORDER BY clause applies to each partition to specify the order of rows.

Example 1

Let us understand how MySQL dense_rank() function works. So, first, create a table that contains the following data:

Table: employees

MySQL Ranking Functions

This statement uses the dense_rank() function for assigning the rank value for each row.

After executing the above statement, we will get the following output:

MySQL Ranking Functions

Example 2

Let us see another example that divides the result set into partitions. The following statement uses dense_rank() function to assign the value on each row and divide the result set into partition using emp_age:

After the successful execution of the above query, we will get the following output:

MySQL Ranking Functions

MySQL rank()

It is a function that assigns a rank for every row within a partition or result set with gaps. The rank of rows is always not-assigned in a consecutive order (i.e., increased by one from the previous row). Sometimes you will get a tie between the values, then the rank() function will assign it with the same rank, and the next rank value will be its previous rank plus a number of duplicate numbers.

The following are the syntax of rank():

In the above syntax, the PARTITION BY clause partition the result set return by FROM clause, and then the rank() function applies on each partition and re-initialized when the partition boundary crosses other partition. Next, the ORDER BY clause applies on each partition to sorts the rows based on one or more columns names.

Let us take a table that we have created previously and see the working of rank() function in MySQL with different examples.

Table: employees

MySQL Ranking Functions

Example 1

This statement uses the rank() function for assigning the rank value for each row.

The above query will give the following output:

MySQL Ranking Functions

Example 2

Let us see another example that divides the result set into partitions. The following statement uses the rank() function to assign the value on each row and divide the result set into partition using emp_age and sorts them based on emp_id:

Executing the above statement, we will get the following output:

MySQL Ranking Functions

MySQL percent_rank()

It is a function that calculates a percentile rank (relative rank) for rows within a partition or result set. This function returns a number from a range of values between 0 and 1.

The following are the syntax of percent_rank():

For a specified row, this function calculates the rank by using the following formula:

Here,

rank: It is the rank of each row returns by rank() function.

total_rows: It represents the total number of rows present in the partition.

NOTE: It is to make sure that when you use this function, you must have to use the ORDER BY clause. Otherwise, all rows are considered duplicates and assigned the same rank, which is 1.

Let us create a table "students" that contains the following data and see the working of percent_rank() function in MySQL.

Table: students

MySQL Ranking Functions

Example 1

This statement uses the percent_rank() function for calculating the rank value for each row order by marks column.

The above query will give the following output:

MySQL Ranking Functions

To see how the above formula works, consider the following query:

It will give the following output:

MySQL Ranking Functions




Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA