HackerRank[MySQL] : Contest Leaderboard

SOOYEON·2022년 2월 2일
0

SQL

목록 보기
24/54

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_idname, 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  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

INNER JOIN

# Try 
SELECT H.hacker_id, name, SUM(score) as total_score
FROM hackers AS H INNER JOIN 
(SELECT hacker_id, MAX(score) AS score 
FROM submissions 
GROUP BY challenge_id, hacker_id) AS S
ON H.hacker_id = S.hacker_id
GROUP BY H.hacker_id , name 
HAVING total_score > 0
ORDER BY total_score DESC , H.hacker_id;

0개의 댓글