Skip to content

Commit 3fe2f77

Browse files
authored
Add files via upload
1 parent b22c79d commit 3fe2f77

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Database/178. Rank Scores.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no "holes" between ranks.
2+
3+
```
4+
+----+-------+
5+
| Id | Score |
6+
+----+-------+
7+
| 1 | 3.50 |
8+
| 2 | 3.65 |
9+
| 3 | 4.00 |
10+
| 4 | 3.85 |
11+
| 5 | 4.00 |
12+
| 6 | 3.65 |
13+
+----+-------+
14+
```
15+
For example, given the above Scores table, your query should generate the following report (order by highest score):
16+
17+
```
18+
+-------+------+
19+
| Score | Rank |
20+
+-------+------+
21+
| 4.00 | 1 |
22+
| 4.00 | 1 |
23+
| 3.85 | 2 |
24+
| 3.65 | 3 |
25+
| 3.65 | 3 |
26+
| 3.50 | 4 |
27+
+-------+------+
28+
```
29+
30+
**SQL**
31+
```
32+
# Write your MySQL query statement below
33+
SELECT s2.Score,s1.Rank From
34+
(
35+
SELECT S1.Score, COUNT(*) as Rank
36+
FROM
37+
(SELECT DISTINCT Score from Scores) as S1,
38+
(SELECT DISTINCT Score from Scores) as S2
39+
Where S1.Score<=S2.Score
40+
Group By S1.Score
41+
) s1 ,Scores s2
42+
WHERE s1.Score = s2.Score Order BY s2.Score desc
43+
```

0 commit comments

Comments
 (0)