Skip to content

Instantly share code, notes, and snippets.

@GGrassiant
Last active April 12, 2021 13:15
Show Gist options
  • Select an option

  • Save GGrassiant/e7a9a3551f9929221ef37eb586a5131a to your computer and use it in GitHub Desktop.

Select an option

Save GGrassiant/e7a9a3551f9929221ef37eb586a5131a to your computer and use it in GitHub Desktop.
A. SQL Grouping and Subquery
select h.hacker_id, h.name, sum(max_score.score) as total_score
from hackers as h
inner join
/* find max_score per user/challenge */
(select hacker_id, max(score) as score from submissions group by challenge_id, hacker_id) max_score
on h.hacker_id = max_score.hacker_id
group by h.hacker_id, h.name
-- /* don't accept hackers with total_score=0 */
having total_score > 0
-- /* finally order as required */
order by total_score desc, h.hacker_id;
SELECT h.hacker_id, h.name
FROM submissions s
JOIN challenges c
ON s.challenge_id = c.challenge_id
JOIN difficulty d
ON c.difficulty_level = d.difficulty_level
JOIN hackers h
ON s.hacker_id = h.hacker_id
WHERE s.score = d.score
GROUP BY h.hacker_id, h.name
HAVING COUNT(s.hacker_id) > 1
ORDER BY COUNT(s.hacker_id) DESC, s.hacker_id ASC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment