MySQL DENSE_RANK Function

Learn via video courses
Topics Covered

Overview

The DENSE_RANK function in MySQL assigns a rank to each row in a result set depending on the ordering supplied. When there are ties, it does not leave gaps in the ranking sequence, unlike the usual RANK function. Instead, it assigns sequential ranks to rows that are tied. The DENSE_RANK function in MySQL is commonly used in situations where data must be ranked without gaps, such as in leaderboards or competitive rankings. It offers a concise and quick method for producing dense ranking results within MySQL queries.

Syntax of MySQL DENSE_RANK Function

The DENSE_RANK function in MySQL has the following general syntax:

Let's break down the syntax:

  • PARTITION_BY (optional):
    Specifies the columns to partition the result set. The DENSE_RANK() function will restart the rank count for each unique combination of partition columns.
  • ORDER_BY:
    Specifies the column(s) to order the rows by.
  • dense_rank:
    Alias for the calculated dense rank column.

Parameters of MySQL DENSE_RANK Function

The following parameters are typically passed to the DENSE_RANK function in MySQL:

  1. ORDER BY clause (required):
    Specifies the column(s) used to determine order for ranking. It specifies the sorting criteria that are used to determine ranks.
  2. PARTITION BY clause(optional):
    Specifies one or more columns to partition the result set. For each unique combination of values in the partition columns, the DENSE_RANK() function will restart the rank count. If this parameter is not specified, the entire result set is handled as a single partition.

You can change how the dense ranks are assigned within the result set by specifying various partition and order columns.

Return Value of MySQL DENSE_RANK Function

MySQL DENSE_RANK() returns an integer value that represents the dense rank assigned to each row in a result set. The dense rank values are all consecutive integers beginning with 1, with no gaps in the ranking sequence.

The DENSE_RANK() method returns a value based on the order supplied in the ORDER_BY clause. Rows with the same order column values will have the same dense rank value. The following row with a different value will be assigned the next successive rank.

Exceptions of MySQL DENSE_RANK Function

In general, the SQL DENSE_RANK() method works as expected, returning dense rank values depending on the supplied ordering. However, there are a few exceptions to consider:

  1. Ties with the same rank:
    If the ordering columns have ties, the DENSE_RANK() method will assign the same rank to those rows. If two rows have the same value and are ranked as 1, the next row will be ranked as 2. Following the knotted rows, the ranks will be ordered in ascending order.
  2. Gaps in dense ranks caused by skipped values:
    If the ordering column(s) have gaps or missing values, the DENSE_RANK() method will nevertheless assign consecutive ranks based on the actual values encountered during the ranking. It does not use intermediate ranks to fill in the gaps. Only when there is a significant change in the ordering values will the rankings be incremented.
  3. Partitions and ranking within partitions:
    When combined with the DENSE_RANK() function, the PARTITION_BY clause resets the rank count for each unique combination of values in the partition columns. This means that inside each division, ranks will be assigned independently. For each partition, the ranks will begin at 1 and go sequentially within that partition.

How does the DENSE_RANK Function in MySQL Work?

The SQL DENSE_RANK() function is a window function that assigns a unique rank to each different value in a result set by leaving no gaps in the ranking sequence. The function operates in the following manner:

  1. Sorting the result set:
    The DENSE_RANK() function first sorts the rows in the result set using the ORDER_BY clause's provided ordering columns. Depending on the sorting criteria, the order can be ascending (the default) or downward.
  2. Assigning ranks:
    The DENSE_RANK() method assigns a rank value of 1 to the first row in the sorted result set. The rank reflects the row's position inside the ordered result set.
  3. Handling Ties:
    Ties are handled by assigning the same rank to the following row(s) that have the same value as the previous row(s) based on the ordering columns. This assures that the ranking sequence has no gaps.
  4. Incrementing ranks:
    As the DENSE_RANK() function traverses the result set, it increases the rank value by one if the ordering columns' values change noticeably. This assures that each value is assigned a distinct rank, with the ranks being consecutive integers.

Example of MySQL DENSE_RANK Function()

Now, Let's look at the following example for MySQL DENSE_RANK Function. Let's say we have the following table named Students.

IDValue
110
220
310
430
510
620

If you apply the DENSE_RANK function in MySQL with the ORDER_BY clause on the "Value" column, according to the query.

The result would be:

IDValueDense_Rank
1101
2202
3101
4303
5101
6202

Rows with a value of 10 have a dense rank of one, rows with a value of twenty have a dense rank of two, and rows with a value of thirty have a dense rank of three. There are no pauses in the ranking process, and the ranks are assigned consecutively.

Now, Let's see an example in which the PARTITION_BY clause is also used.

StudentIDNameScoreClass
1Alice90A
2Bob85B
3Alice80A
4Bob85B
5Charlie95A

Now, let's use the DENSE_RANK() function to assign ranks to the students within each class based on their scores:

The output will be:

StudentIDNameScoreClassdense_rank
1Alice90A1
2Bob85B2
3Alice80A3
4Bob85B1
5Charlie95A1

The DENSE_RANK function in MySQL is used within each class partition in this example. Charlie obtains a rank of 1 in Class A since he has the greatest score. Alice obtains a rank of 2 for having the second-highest score in Class A. Bob, who has the best score in Class B, is also ranked first in that class.

Conclusion

  • MySQL's DENSE_RANK() method assigns rankings to rows in a result set based on the ordering supplied.
  • The syntax of the function includes an optional PARTITION_BY clause for partitioning the result set and an ORDER_BY clause for determining the ranking order.
  • The DENSE_RANK() method returns an integer value representing each row's dense rank. The rankings are a series of consecutive integers beginning with 1, with no pauses in the sequence.
  • Ties with the same rank, gaps produced by skipped values in the ordering column(s), and partitions that reset the rank count for each unique combination of partition columns are all exceptions to consider.
  • The DENSE_RANK() method sorts the result set based on the ordering columns provided, assigning ranks beginning with 1 to the first row, handling ties by assigning the same rank to tied rows, and incrementing ranks when the ordering column values change.

See Also

  1. Scalar Functions in SQL
  2. SQL Data Types
  3. Joins in SQL
  4. Aggregate Functions in SQL
  5. Subqueries in SQL