Skip to content

Commit afddc04

Browse files
committed
feature:[MySQL].178.rank scores.
1 parent c806065 commit afddc04

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ LeetCode
3939
| --- | ----------------------------------------------------------------------------------------------------------------------------------- | ---------- | ---------------------------------------------------------------------- |
4040
| 175 | [Combine Two Tables](https://leetcode.com/problems/combine-two-tables/) | Easy | [MySQL](./database/175.combine-two-tables.sql) |
4141
| 176 | [Second Highest Salary](https://leetcode.com/problems/second-highest-salary/description/) | Easy | [MySQL](./database/176.second-highest-salary.sql) |
42+
| 178 | [Rank Scores](https://leetcode.com/problems/rank-scores/description/) | Medium | [MySQL](./database/178.rank-scores.sql) |
4243
| 180 | [Consecutive Numbers](https://leetcode.com/problems/consecutive-numbers/description/) | Medium | [MySQL](./database/180.consecutive-numbers.sql) |
4344
| 181 | [Employees Earning More Than Their Managers](https://leetcode.com/problems/employees-earning-more-than-their-managers/description/) | Easy | [MySQL](./database/181.employees-earning-more-than-their-managers.sql) |
4445
| 182 | [Duplicate Emails](https://leetcode.com/problems/duplicate-emails/description/) | Easy | [MySQL](./database/182.duplicate-emails.sql) |

Diff for: database/178.rank-scores.sql

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
--
2+
-- @lc app=leetcode id=178 lang=mysql
3+
--
4+
-- [178] Rank Scores
5+
--
6+
-- https://leetcode.com/problems/rank-scores/description/
7+
--
8+
-- database
9+
-- Medium (44.63%)
10+
-- Likes: 758
11+
-- Dislikes: 139
12+
-- Total Accepted: 114.3K
13+
-- Total Submissions: 248.7K
14+
-- Testcase Example: '{"headers": {"Scores": ["Id", "Score"]}, "rows": {"Scores": [[1, 3.50], [2, 3.65], [3, 4.00], [4, 3.85], [5, 4.00], [6, 3.65]]}}'
15+
--
16+
-- Write a SQL query to rank scores. If there is a tie between two scores, both
17+
-- should have the same ranking. Note that after a tie, the next ranking number
18+
-- should be the next consecutive integer value. In other words, there should
19+
-- be no "holes" between ranks.
20+
--
21+
--
22+
-- +----+-------+
23+
-- | Id | Score |
24+
-- +----+-------+
25+
-- | 1 | 3.50 |
26+
-- | 2 | 3.65 |
27+
-- | 3 | 4.00 |
28+
-- | 4 | 3.85 |
29+
-- | 5 | 4.00 |
30+
-- | 6 | 3.65 |
31+
-- +----+-------+
32+
--
33+
--
34+
-- For example, given the above Scores table, your query should generate the
35+
-- following report (order by highest score):
36+
--
37+
--
38+
-- +-------+---------+
39+
-- | score | Rank |
40+
-- +-------+---------+
41+
-- | 4.00 | 1 |
42+
-- | 4.00 | 1 |
43+
-- | 3.85 | 2 |
44+
-- | 3.65 | 3 |
45+
-- | 3.65 | 3 |
46+
-- | 3.50 | 4 |
47+
-- +-------+---------+
48+
--
49+
--
50+
-- Important Note: For MySQL solutions, to escape reserved words used as column
51+
-- names, you can use an apostrophe before and after the keyword. For example
52+
-- `Rank`.
53+
--
54+
--
55+
56+
-- @lc code=start
57+
-- Write your MySQL query statement below
58+
59+
SELECT
60+
Score,
61+
(SELECT count(distinct Score)
62+
FROM Scores
63+
WHERE Score >= s.Score)
64+
`Rank`
65+
FROM Scores s
66+
ORDER BY Score desc
67+
68+
-- @lc code=end

0 commit comments

Comments
 (0)