Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Table: Scores
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| score | decimal |
+-------------+---------+
id is the primary key for this table.
Each row of this table contains the score of a game. Score is a floating point value with two decimal places.
Write an SQL query to rank the scores. The ranking should be calculated according to the following rules:
Return the result table ordered by score
in descending order.
The query result format is in the following example.
Example 1:
Input:
Scores table:
+----+-------+
| id | score |
+----+-------+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
+----+-------+
Output:
+-------+------+
| score | rank |
+-------+------+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
+-------+------+
SELECT
Score,
@rank := @rank + (@prev <> (@prev := Score)) Rank
FROM
Scores,
(SELECT @rank := 0, @prev := -1) init
ORDER BY Score desc
SELECT s.Score as Score,
DENSE_RANK() OVER (ORDER BY s.Score DESC) as Rank
FROM Scores s
select
s.score Score ,
r.rn Rank
from
Scores s,
(select s.score sc, rownum rn from
(select s.score from Scores s
group by s.Score order by s.score desc) s) r
where s.score = r.sc
In our experience, we suggest you solve this Rank Scores LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Rank Scores LeetCode Solution
I hope this Rank Scores LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>