SQL & DB/HackerRank SQL Problem

[HackerRank SQL] Top Competitors

YSY^ 2021. 3. 1. 20:39

www.hackerrank.com/challenges/full-score/problem

 

Top Competitors | HackerRank

Query a list of top-scoring hackers.

www.hackerrank.com

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.

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. 

  • Difficulty: The difficult_level is the level of difficulty of the challenge, and score is the score of the challenge for the difficulty level. 

  • Challenges: The challenge_id is the id of the challenge, the hacker_id is the id of the hacker who created the challenge, and difficulty_level is the level of difficulty of the challenge. 

  • 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 that the submission belongs to, and score is the score of the submission. 


Answer

select h.hacker_id, h.name # respective hacker_id and name of hackers
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 # who achieved full scores for more than one challenge
order by count(*) desc, h.hacker_id;

 

Result

728x90
반응형