Contest Leaderboard - LeetCode

Pepzera·2026년 1월 29일

SQL코딩테스트

목록 보기
9/18

Contest Leaderboard 문제

출처 : LeetCode Contest Leaderboard

Q.

You did such a great job helping Julia with her last coding contest challenge that she wants you to work on this one, too!
The total score of a hacker is the sum of their maximum scores for all of the challenges. Write a query to print the hacker_id, name, and total score of the hackers ordered by the descending score. If more than one hacker achieved the same total score, then sort the result by ascending hacker_id. Exclude all hackers with a total score of 0 from your result.

Input Format

The following tables contain contest data:

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

  • 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 for which the submission belongs to, and score is the score of the submission.

Sample Input

Hackers Table:

Submissions Table:

Sample Output

4071 Rose 191
74842 Lisa 174
84072 Bonnie 100
4806 Angela 89
26071 Frank 85
80305 Kimberly 67
49438 Patrick 43

Explanation
Hacker 4071 submitted solutions for challenges 19797 and 49593, so the total score = 95 + max(43,96) = 191

Hacker 74842 submitted solutions for challenges 19797 and 63132, so the total score = max(98,5) + 76 = 174

Hacker 84072 submitted solutions for challenges 49593 and 63132, so the total score = 100 + 0 = 100

The total scores for hackers 4806, 26071, 80305, and 49438 can be similarly calculated.


질문

각 해커(hacker)의 총 점수(sum score)를 계산하여 Leaderboard 형태로 출력!
단, 총 점수가 0인 해커는 제외
정렬 기준은 총 점수 내림차순, 총 점수가 같으면 hacker_id 기준 오름차순

내 답안 📕

SELECT hacker_id
     , name
     , SUM(score) AS sum_score
FROM (SELECT s.hacker_id AS hacker_id
           , s.challenge_id AS challenge_id
           , h.name AS name
           , MAX(s.score) AS score
      FROM Submissions AS s
        INNER JOIN Hackers AS h ON s.hacker_id = h.hacker_id
      GROUP BY s.hacker_id, s.challenge_id, h.name
      ORDER BY s.hacker_id ASC
) AS NT
GROUP BY hacker_id, name
  HAVING sum_score != 0
ORDER BY sum_score DESC, hacker_id ASC;

0개의 댓글