[HackerRank] Contest Leaderboard

mzzzi·2022년 4월 25일
0

HackerRank SQL

목록 보기
50/52
post-thumbnail

MySQL > Contest Leaderboard


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 Output

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

My Answer


SELECT H.HACKER_ID, H.NAME, SUM(S.M_SCORE)
FROM HACKERS H JOIN (SELECT HACKER_ID, CHALLENGE_ID, MAX(SCORE) M_SCORE
                     FROM SUBMISSIONS
                     GROUP BY HACKER_ID, CHALLENGE_ID) S
     ON H.HACKER_ID = S.HACKER_ID
GROUP BY H.HACKER_ID, H.NAME
HAVING SUM(S.M_SCORE) > 0
ORDER BY SUM(S.M_SCORE) DESC, H.HACKER_ID
profile
무럭무럭 성장하라🌳

0개의 댓글