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.
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.
Code
#1
Idea
4중 Join 문제였다;;
(inner) join 만을 여러 개 쓴다면 join 의 순서는 상관이 없다. (순서 달라져도 결과 똑같이 나옴)
테이블끼리 어떻게 엮을 지가 중요하다.
on 을 잘 사용해서 모든 테이블을 엮어주면 성공이다.
Code
select h.hacker_id, h.name
from submissions as s
join challenges as c
on s.challenge_id = c.challenge_id
join difficulty as d
on c.difficulty_level = d.difficulty_level
join hackers as h
on s.hacker_id = h.hacker_id
where d.score = s.score
group by h.hacker_id, h.name
having count(h.hacker_id)>1
order by count(h.hacker_id) desc, h.hacker_id asc
SQL
복사
Solution
Commentary
#2
Idea
Code
SQL
복사