HackerRank SQL

Isabelle
JEN-LI CHEN IN DATA SCIENCE
1 min readJul 17, 2020

Top Competitors

Problem:

Julia just finished conducting a coding contest, and she needs your help assembling the leaderboard! Write a query to print the respective hacker_id and name of hackers who achieved full scores for more than one challenge. Order your output in descending order by the total number of challenges in which the hacker earned a full score. If more than one hacker received full scores in same number of challenges, then sort them by ascending hacker_id.

Sample Output:

90411 Joe

Logic:

The output only have hacker_id and name, but Submissions table has only hacker_id while Hackers table has only name. So, we have to write a query to select hacker_id and name by joining Submissions table with Hackers, Challenges and Difficulty tables.

Next, filter the full score users by using s.score = d.score.

Then select count(*) > 1, group by h.hacker_id, h.name, and then order by count desc and hacker_id asc

Solution:

select h.hacker_id, h.name from Submissions as s join Hackers as h 
on s.hacker_id = h.hacker_id
join Challenges as c on s.challenge_id = c.challenge_id
join Difficulty as d on c.Difficulty_level = d.Difficulty_level
where s.score = d.score
group by h.hacker_id, h.name
having count(*) > 1
order by count(*) desc, h.hacker_id;

Question Link

Reference

--

--