Thursday, May 25, 2017

SQL Basic join - Contest Leaderboard

Problem
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 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 .
Hacker 74842 submitted solutions for challenges 19797 and 63132, so the total score
Hacker 84072 submitted solutions for challenges 49593 and 63132, so the total score .
The total scors for hackers 4806, 26071, 80305, and 49438 can be similarly calculated.

My Answer
SELECT h.hacker_id,
       h.name,
       Sum(top) AS total
FROM   hackers AS h
       INNER JOIN(SELECT hacker_id,
                         challenge_id,
                         Max(score) AS top
                  FROM   submissions
                  GROUP  BY hacker_id,
                            challenge_id
                  ORDER  BY top DESC) AS s
               ON h.hacker_id = s.hacker_id
GROUP  BY h.hacker_id,
          h.name
HAVING total > 0
ORDER  BY total DESC,
          h.hacker_id 

1 comment:

  1. Your code did not pass this test case.

    Your Output (stdout)

    Msg 156, Level 15, State 1, Server WIN-ILO9GLLB9J0, Line 4
    Incorrect syntax near the keyword 'top'.
    Msg 156, Level 15, State 1, Server WIN-ILO9GLLB9J0, Line 8
    Incorrect syntax near the keyword 'top'.
    Compiler Message

    Runtime Error

    ReplyDelete