문제 설명
Julia asked her students to create some coding challenges. Write a query to print the hacker_id, name, and the total number of challenges created by each student. Sort your results by the total number of challenges in descending order. If more than one student created the same number of challenges, then sort the result by hacker_id. If more than one student created the same number of challenges and the count is less than the maximum number of challenges created, then exclude those students from the result.
Input Format
Column | Type |
---|---|
hacker_id | Integer |
name | String |
Column | Type |
---|---|
challenge_id | Integer |
hacker_it | Integer |
이번 문제는 설명으로봐서 절대 이해가 안되서 예시 input, output 을 꼭 봐야한다.
어려운 부분
SELECT H.hacker_id, H.name, COUNT(C.challenge_id) AS challenges_created
FROM Hackers H
INNER JOIN Challenges C on H.hacker_id = C.hacker_id
GROUP BY C.hacker_id, H.name
정답
SELECT H.hacker_id, H.name, COUNT(C.challenge_id) AS challenges_created
FROM Hackers H
INNER JOIN Challenges C on H.hacker_id = C.hacker_id
GROUP BY C.hacker_id, H.name
Having
# 최대값
challenges_created = (SELECT COUNT(C1.challenge_id) FROM Challenges AS C1
GROUP BY C1.hacker_id ORDER BY COUNT(*) DESC LIMIT 1) OR
# 최대값이 아닌것 중, 중복되지 않은것들
challenges_created NOT IN (SELECT COUNT(C2.challenge_id)FROM Challenges AS C2
GROUP BY C2.hacker_id HAVING C2.hacker_id != C.hacker_id)
ORDER BY challenges_created Desc, C.hacker_id;