[SQL] Top Competitors

μˆœλ™Β·2022λ…„ 5μ›” 18일
0

βœ… 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 2 space-separated values.


πŸ’» 풀이

SELECT S.hacker_id, H.name
FROM Submissions AS S INNER JOIN Hackers AS H USING(hacker_id)
    INNER JOIN Challenges AS C USING(challenge_id)
    INNER JOIN Difficulty AS D USING(difficulty_level)
WHERE D.score = S.score
GROUP BY S.hacker_id, H.name
HAVING COUNT(S.hacker_id) > 1
ORDER BY COUNT(S.hacker_id) DESC, S.hacker_id ASC;

πŸ’‘ Idea

  • 4개의 ν…Œμ΄λΈ”μ„ 연속 JOIN
  • 각각의 ν…Œμ΄λΈ”μ˜ hacker_id 컬럼의 μ˜λ―Έκ°€ 닀름에 μ£Όμ˜ν•œλ‹€.
    λ‘˜ μ΄μƒμ˜ κ³Όμ œμ— λŒ€ν•΄ λ§Œμ μ„ νšλ“ν•œ hacker_idκ°€ ν•„μš”ν•˜λ―€λ‘œ μ œμΆœν•œ hacker_id 정보가 λ‹΄κΈ΄ Submissions ν…Œμ΄λΈ”μ˜ hacker_id μ»¬λŸΌμ„ μ€‘μ‹¬μœΌλ‘œ μ‘°μΈν•œλ‹€.
  • HAVINGμ ˆμ„ 톡해 λ‘˜ μ΄μƒμ˜ 과제λ₯Ό μ œμΆœν•œ(도전 횟수) hackerλ₯Ό μ°ΎλŠ”λ‹€.
  • WHEREμ ˆμ„ 톡해 full scoreλ₯Ό 받은 쑰건을 μ„€μ •ν•œλ‹€.
  • ORDER BYλŠ” 총 도전 νšŸμˆ˜μ— λ”°λ₯Έ λ‚΄λ¦Όμ°¨μˆœ, 도전 νšŸμˆ˜κ°€ 동일할 경우 hacker_id둜 μ˜€λ¦„μ°¨μˆœν•œλ‹€.

0개의 λŒ“κΈ€