1. 문제
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 2 space-separated values.
2. 시행착오
SELECT h.hacker_id, h.name
FROM submissions AS s
LEFT JOIN hackers AS h ON s.hacker_id = h.hacker_id
LEFT JOIN challenges AS c ON s.challenge_id = c.challenge_id
LEFT JOIN difficulty AS d ON c.difficulty_level = d.difficulty_level
WHERE s.score = d.score
GROUP BY h.hacker_id
HAVING COUNT(*) >= 2
ORDER BY COUNT(s.submission_id) DESC, h.hacker_id
- Error Code: 1055 발생 이유: 해당 열을 SELECT 하여 출력하고 싶을 때, 해당 열의 집계 함수 이거나 GROUP BY 절에 있어야 한다.
3. 답
SELECT h.hacker_id, h.name
FROM submissions AS s
LEFT JOIN hackers AS h ON s.hacker_id = h.hacker_id
LEFT JOIN challenges AS c ON s.challenge_id = c.challenge_id
LEFT 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(*) >= 2
ORDER BY COUNT(s.submission_id) DESC, h.hacker_id
4. KEY POINT
- ORDER BY에서 2개의 조건이 나올때, 1번째 조건에 따라 정렬을 먼저 하고, 그다음 1번째 조건에 의해 같은 선상에 위치하는 것을 2번째 조건에 의해 정렬한다.
- SELECT, HAVING, ORDER BY 목록에 있는 컬럼은 집계 함수 이거나 GROUP BY 절에 있어야 한다.
www.hackerrank.com/challenges/full-score/problem?h_r=internal-search
댓글