leetcode(178) 分数排名

leetcode(178) 分数排名_第1张图片
方法一:
分情况,再连接:

# Write your MySQL query statement below
SELECT S1.Score,
(
    SELECT COUNT(DISTINCT S2.Score)
    FROM Scores S2
    WHERE S2.Score>=S1.Score
)AS Rank
FROM Scores S1
ORDER BY Score DESC
;

方法二:
自连接:

# Write your MySQL query statement below
SELECT S1.Score, 
       COUNT(DISTINCT S2.Score) AS Rank 
FROM Scores S1,Scores S2
WHERE S2.Score>=S1.Score
GROUP BY S1.Id
ORDER BY S1.Score DESC
;

你可能感兴趣的:(LeetCode,数据库练习题)