Rank Scores SQL Challenge - MySQL Solution

1. Introduction

In this blog post, we'll explore how to use MySQL to rank scores in a database. This is a common task in SQL that involves assigning a rank to each row within a partition of a result set.


The challenge is to write a MySQL query that assigns a rank to each score in the database table. The rank should be calculated based on the score in descending order.

Database Table:

Scores Table:
id score
1 90
2 87
3 85

3. MySQL Solution

    DENSE_RANK() OVER (ORDER BY score DESC) as `rank`
FROM Scores;


score rank
90 1
87 2
85 3


1. The DENSE_RANK() window function is used to assign a rank to each score.

2. Scores are ordered in descending order.

3. Each distinct score gets a unique rank, with ties receiving the same rank.

4. This approach ensures that there are no gaps in ranking numbers.