HAZEL

[SQL : HackerRank] Top Competitors 본문

DATA ANALYSIS/SQL

[SQL : HackerRank] Top Competitors

Rmsid01 2021. 2. 28. 10:07

Top Competitors


>> 문제

 

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. 


Sample Input

Hackers Table: 

 Difficulty Table: 

 Challenges Table: 

 Submissions Table: 

Sample Output

90411 Joe

 

Explanation

Hacker 86870 got a score of 30 for challenge 71055 with a difficulty level of 2, so 86870 earned a full score for this challenge.

Hacker 90411 got a score of 30 for challenge 71055 with a difficulty level of 2, so 90411 earned a full score for this challenge.

Hacker 90411 got a score of 100 for challenge 66730 with a difficulty level of 6, so 90411 earned a full score for this challenge.

Only hacker 90411 managed to earn a full score for more than one challenge, so we print the their hacker_id and name as  space-separated values.

 

 

>> 실패한 코드 

select h.hacker_id, h.name    
from hackers H
    left join challenges C on H.hacker_id = C.hacker_id
    left join Difficulty D on C.difficulty_level = D.difficulty_level
    left join Submissions S on C.challenge_id = S.challenge_id 
where D.score = S.score 
group by h.hacker_id, h.name
having count(distinct s.submission_id)>1
ORDER BY count(distinct s.submission_id) Desc , h.hacker_id

hackers 의 id를 기준으로 left join 을 해주려고 했다. .. 하지만 그러면, 정답이 꼬이는 것같다.

전체 submission에 대한 값이 나오지 않아서 output이 실제 정답보다 적게 나왔다. 

join 이랑 group by랑 합쳐지니깐, 조금 헷갈려졌다..ㅠㅠ 

 

※ 집계함수의 조건은 무조건 having 절이다!! ㅠㅠㅠ !!!!!!! 쉽다고 넘기지 말자! 명심하자

※ join 할때, 어떤 조인을 해야하는지 잘 생각해보고 하자. 

 

>> 정답 코드 

SELECT H.hacker_id, H.name
FROM Submissions S
    INNER JOIN hackers H on S.hacker_id = H.hacker_id
    INNER JOIN Challenges C on S.challenge_id = C.challenge_id
    INNER JOIN difficulty D on C.difficulty_level = D.difficulty_level
WHERE S.score = D.score
group by h.hacker_id, h.name
having count(distinct s.submission_id) > 1 
order by count(distinct s.submission_id) desc , h.hacker_id

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

 

Top Competitors | HackerRank

Query a list of top-scoring hackers.

www.hackerrank.com