LeetCode - 178. Rank Scores (MySQL)

조민수·2024년 6월 4일
0

LeetCode

목록 보기
10/61

Medium, SQL - DENSE_RANK()

RunTime : 514 ms


문제

Write a solution to find the rank of the scores. The ranking should be calculated according to the following rules:

  • The scores should be ranked from the highest to the lowest.
  • If there is a tie between two scores, both should have the same ranking.
  • After a tie, the next ranking number should be the next consecutive integer value.
    In other words, there should be no holes between ranks.

Return the result table ordered by score in descending order.

The result format is in the following example.


풀이

  • RANK()는 1등이 둘이면, 1, 1, 3 ... 으로 순위를 부여한다.
  • DENSE_RANK()는 1등이 둘이면, 1, 1, 2 ...으로 부여함.
SELECT score,
DENSE_RANK() OVER (ORDER BY score DESC) AS 'rank'
FROM Scores
ORDER BY score DESC
profile
사람을 좋아하는 Front-End 개발자

0개의 댓글