Data & BI/SQL Coding Test

[LeetCode](중급) 178. Lank Score (MS SQL)

무료한하늘 2025. 6. 12. 09:35
728x90

 

문제

Column Name Type
id int
score decimal
id is the primary key (column with unique values) for this table.
Each row of this table contains the score of a game. 
Score is a floating point value with two decimal places.

 

score 별 rank 를 매기고 각 score 별 순위를 score 별 내림차순으로 출력해라.

 

풀이

SELECT A.score, R.idx as rank FROM Scores A
LEFT OUTER JOIN (SELECT score, ROW_NUMBER() OVER (ORDER BY score DESC) AS idx 
FROM Scores group by score) R ON A.score = R.score
order by A.score desc

 

score 별 내림차순 후 인덱스 열 추가를 통해 순위 열을 생성하고 해당 테이블을 조인하여 출력하였다.

300x250