본문 바로가기
MYSQL/HakerRank_Medium

Top Competitors

by 수스리 2025. 4. 17.

문제

 

Julia just finished conducting a coding contest, and she needs your help assembling the leaderboard! Write a query to print the respective hacker_id and name of hackers who achieved full scores for more than one challenge. Order your output in descending order by the total number of challenges in which the hacker earned a full score. If more than one hacker received full scores in same number of challenges, then sort them by ascending hacker_id.

 

Julia가 코딩 대회를 마쳤고, 리더보드를 만드는데 당신의 도움이 필요합니다! 하나 이상의 챌린지에서 만점을 받은 해커들의 hacker_id와 name을 출력하는 쿼리를 작성하세요. 출력 결과는 해커가 만점을 받은 챌린지의 총 개수를 기준으로 내림차순으로 정렬하세요. 만약 여러 해커가 같은 수의 챌린지에서 만점을 받았다면, hacker_id 기준으로 오름차순 정렬하세요.

 

The following tables contain contest data:

  • Hackers: The hacker_id is the id of the hacker, and name is the name of the hacker.

Hackers: hacker_id는 해커의 id이고, name은 해커의 이름입니다.

 

Difficulty: The difficult_level is the level of difficulty of the challenge, and score is the maximum score that can be achieved for a challenge at that difficulty level.

"Difficulty: difficult_level은 챌린지의 난이도 수준이고, score는 해당 난이도 수준의 챌린지에서 획득할 수 있는 최대 점수입니다."

 

Challenges: The challenge_id is the id of the challenge, the hacker_id is the id of the hacker who created the challenge, and difficulty_level is the level of difficulty of the challenge.

"Challenges: challenge_id는 챌린지의 id이고, hacker_id는 챌린지를 만든 해커의 id이며, difficulty_level은 챌린지의 난이도 수준입니다."

 

 

Submissions: The submission_id is the id of the submission, hacker_id is the id of the hacker who made the submission, challenge_id is the id of the challenge that the submission belongs to, and score is the score of the submission.

"Submissions: submission_id는 제출물의 id이고, hacker_id는 제출한 해커의 id이며, challenge_id는 제출물이 속한 챌린지의 id이고, score는 제출물의 점수입니다."

SELECT S.hacker_id, name
FROM Hackers H
JOIN Submissions S ON H.hacker_id = S.hacker_id
JOIN Challenges C ON C.challenge_id = S.challenge_id
JOIN Difficulty D ON D.difficulty_level = C.difficulty_level
WHERE S.score = D.score
GROUP BY S.hacker_id, name
HAVING COUNT(*) >= 2
ORDER BY COUNT(*) DESC, hacker_id ASC

크게 어려운 문제는 아니다. 다만 more than 은 이상이 아닌 초과를 말 한다.

 

<2025년 5월 2일 복습>

SELECT hacker_submission.hacker_id, hacker_submission.name
FROM (
    SELECT S.submission_id, S.hacker_id, H.name, S.challenge_id, S.score
    FROM Hackers H
    JOIN Submissions S ON H.hacker_id = S.Hacker_id
) AS hacker_submission
JOIN (
    SELECT C.challenge_id, C.difficulty_level, D.score
    FROM Challenges C
    JOIN Difficulty D ON C.difficulty_level = D.difficulty_level
) AS TEMP ON hacker_submission.score = TEMP.score and
            hacker_submission.challenge_id = TEMP.challenge_id
GROUP BY hacker_submission.hacker_id, hacker_submission.name
HAVING COUNT(DISTINCT hacker_submission.challenge_id) > 1
ORDER BY COUNT(DISTINCT hacker_submission.challenge_id) DESC, hacker_submission.hacker_id ASC;

복습 코드를 더 어렵게 짰네...