https://www.hackerrank.com/challenges/contest-leaderboard/problem?isFullScreen=true
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.
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.
I was trying to solve this hackerrank problem and I can’t figure out what mistake am I making.
select h.hacker_id,h.name,sum(s.score) as total
from hackers as h inner join submissions as s on h.hacker_id=s.hacker_id
group by h.hacker_id,h.name
having total in
(
select sum(max(score))
from
( select max(score)
from submissions
group by hacker_id,challenge_id
)as highest
group by hacker_id
having sum(total)<>0
)
order by sum(s.score),h.hacker_id;
Any help would be highly appreciated.
2
Answers
The original query contained several inconsistencies, notably in the use of the
HAVING
clause withSUM
andMAX
. Use aLEFT JOIN
to ensure that all hackers from the "Hackers" table are included in the results, even if they haven’t submitted solutions. Then, use a subquery to obtain the maximum score for each (hacker_id
,challenge_id
) pair. Finally, group the results byhacker_id
andname
, calculate the sum of the maximum scores (total_score
), exclude hackers with atotal_score of 0
, and then order the result bytotal_score
in descending order, followed by hacker_id in ascending order in case of a tie intotal_score
.total
comes from theselect
in the outer query, so you couldn’t use it in thehaving
, nor does it make sense to use it again in the innerhaving
.You also can’t use
sum(max(score))
. I suspect you actually meant to use a column alias on the innermost query and then refer to it again in the intermediate level.Either way, this doesn’t look right. There is no need for the extra self-join on
submission
, you can do it in without that. You just need to pre-aggregate the max scores, then join it.You can also do it in just two levels, but then you need to go back to aggregating on
hacker_id
, which may be inefficient.