inblog logo
|
stupefyee
    SQL문제풀기

    [SQL 문제 풀이] Rank Scores (순위 점수)

    Stupefyee's avatar
    Stupefyee
    May 30, 2025
    [SQL 문제 풀이] Rank Scores (순위 점수)
    Contents
    내가 작성한 쿼리
    Rank Scores - LeetCode
    Can you solve this real interview question? Rank Scores - Table: Scores +-------------+---------+ | 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.   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.   Example 1: Input: Scores table: +----+-------+ | id | score | +----+-------+ | 1 | 3.50 | | 2 | 3.65 | | 3 | 4.00 | | 4 | 3.85 | | 5 | 4.00 | | 6 | 3.65 | +----+-------+ Output: +-------+------+ | score | rank | +-------+------+ | 4.00 | 1 | | 4.00 | 1 | | 3.85 | 2 | | 3.65 | 3 | | 3.65 | 3 | | 3.50 | 4 | +-------+------+
    Rank Scores - LeetCode
    https://leetcode.com/problems/rank-scores/description/
    Rank Scores - LeetCode
    notion image
    점수의 순위를 찾기 위한 솔루션을 작성합니다. 순위는 다음 규칙에 따라 계산해야 합니다: * 점수는 최고점부터 최저점까지 매겨야 합니다. * 두 점수 사이에 동점이 있는 경우, 두 점수 모두 동일한 순위를 가져야 합니다. * 동점이 된 후 다음 순위 번호는 다음 연속 정수 값이어야 합니다. 즉, 순위 사이에 구멍이 없어야 합니다. 점수 순서대로 결과 테이블을 내림차순으로 반환합니다.
     

    내가 작성한 쿼리

    MySQL, Oracle

    SELECT score, DENSE_RANK() OVER (ORDER BY score DESC) AS "rank" FROM Scores ORDER BY "rank";
    Share article
    Contents
    내가 작성한 쿼리

    stupefyee

    RSS·Powered by Inblog