문제 사이트
아래 문제는 해커랭크에서 추출해온 문제입니다. 문제의 저작권은 해커랭크에 있으며 문제를 풀어보시려면 아래 링크를 클릭해주세요 🙂
Top Competitors | HackerRank
Query a list of top-scoring hackers.
www.hackerrank.com
문제 설명
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.
🎙️줄리아는 코딩 대회를 마쳤고 그녀는 리더보드를 만들려고 한다. 2개 이상의 challenge에서 만점을 받은 해커의 아이디와 이름을 각각 출력해라. 해커가 얻은 만점 점수의 횟수를 기준으로 내림차순 정렬해라. 만점을 받은 challenge 개수가 동일하다면, hacker_id 순으로 오름차순 정렬하라.
문제 풀이
문제를 요약하자면 2개 이상의 challenge에서 만점 받은 해커를 찾아 주어진 기준에 맞게 정렬하는 문제이다.
따라서 먼저 생각해야 할 조건은
1. 만점 : Submissions의 score = Challenges의 difficulty_level -> Difficulty의 score 이어야 함
2. 두 개 이상 : 만점을 받은 횟수가 2개 이상인 참가자를 뽑아야 함, 만점인 사람만 따로 뽑고 Group by하여 카운트 한 후 2개 이상인 참가자 추출
문제 풀이 과정
1. 먼저 겹치는 컬럼들끼리 조인을 해주었다. (보기 쉽도록 select 값은 일단 전체로 해둠)
2. 만점 조건에 맞도록 조건을 걸어주었다.
submissons.score = difficulty.score and challenges.difficulty_level = difficulty.difficulty_level
3. group by를 통해 hacker_id 별로 몇 번의 만점이 있었는지 count 하였다
select *을 했더니 계산할 수 없는 필드값도 있어서 오류가 났다. 그래서 여기부터 id와 name만 추출했다
4. 만점 횟수가 많은 순으로 정렬, hacker_id 오름차순 정렬
최종 답안
select h.hacker_id, h.name
from submissions s
join challenges c on s.challenge_id = c.challenge_id
join difficulty d on c.difficulty_level = d.difficulty_level
join hackers h on s.hacker_id = h.hacker_id
where s.score = d.score and c.difficulty_level = d.difficulty_level
group by h.hacker_id, h.name
having count(h.hacker_id) > 1
order by count(h.hacker_id) desc, h.hacker_id
'SQL > MySQL' 카테고리의 다른 글
[MySQL] HackerRank - Challenges 문제 풀이 (0) | 2024.08.16 |
---|---|
[MySQL] HackerRank - Ollivander's Inventory 문제 풀이 (0) | 2024.08.15 |
[MySQL] 프로그래머스 SQL 문제 + 정답, String, Date 문 (0) | 2024.06.25 |
[MySQL] 프로그래머스 SQL 문제 + 정답, JOIN 문 (0) | 2024.06.18 |
[MySQL] 프로그래머스 SQL 문제 + 정답, IS NULL 문 (0) | 2024.06.15 |