1 minute read

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 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.

My solution

SELECT hacker_id, name, sum(score)
FROM
(
SELECT rownumber,name,hacker_id,challenge_id,score
FROM
(
    SELECT ROW_NUMBER() OVER(PARTITION BY s.hacker_id, s.challenge_id order by s.score DESC) as rownumber, h.name,s.hacker_id,s.challenge_id,s.score
    FROM HACKERS h, SUBMISSIONS s
    WHERE h.hacker_id=s.hacker_id
)
WHERE rownumber=1
)
GROUP BY hacker_id,name
HAVING sum(score) > 0
ORDER BY sum(score) DESC, hacker_id;

Most vote solution

SELECT h.hacker_id, h.name, sum(score)
FROM hackers h,(SELECT hacker_id, max(score) as score
                FROM submissions
                GROUP BY hacker_id, challenge_id) max_score
WHERE h.hacker_id = max_score.hacker_id
GROUP BY h.hacker_id,h.name
HAVING sum(score) > 0
ORDER BY sum(score) DESC, h.hacker_id;

Tags:

Categories:

Updated:

Comments