Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 |
Tags
- python
- dense_rank
- leetcode
- Retention
- 윈도우함수
- MYSQL
- regexp
- 윈도우 함수
- 순위함수
- SQL
- advent of sql
- 서브쿼리
- 독서
- 프로그래머스
- 리텐션
- 취준
- rank
- LEFTJOIN
- row_number
- 누적합
- 데이터리안
- 데이터분석
- 퍼널분석
- 그로스해킹
- SolveSQL
- Datarian
- funnel
- 신입 데이터분석가
- pandas
- 린분석
Archives
- Today
- Total
데이터 분석
178. Rank Scores | Medium 본문
문제
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 |