데이터 분석

178. Rank Scores | Medium 본문

SQL/leetcode

178. Rank Scores | Medium

딱한아이 2025. 2. 1. 17:36
문제

 

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.

테이블 정보
예시

 

score 값을 기준으로 내림차순 정렬하여 순위를 부여하는 문제이다.

단, 값이 동일할 경우 같은 순위를 부여하며 다음 순위는 건너뛰지 않는 것을 원칙으로 한다.

 

따라서 RANK() 함수가 아닌 DENSE_RANK() 함수를 사용하는 것이 적절하다.

 

Solution
SELECT score
     , DENSE_RANK() OVER (ORDER BY score DESC) AS 'rank'
FROM Scores
ORDER BY score DESC

 

💡TIP) 예약어를 Alias로 써야할 때는 따옴표로 감싸주면 에러가 발생하지 않는다

'SQL > leetcode' 카테고리의 다른 글

1393. Capital Gain/Loss | Medium 💡  (0) 2025.02.17
1321. Restaurant Growth | Medium 🔥  (1) 2025.02.01
185. Department Top Three Salaries | Hard  (1) 2025.02.01
180. Consecutive Numbers | Medium  (1) 2025.01.24
511. Game Play Analysys Ι | Easy  (0) 2025.01.24