본문 바로가기
SQL/MySQL

[MySQL] HackerRank - Challenges 문제 풀이

by 취준생 져니 2024. 8. 16.

문제 사이트

아래 문제는 해커랭크에서 추출해온 문제입니다. 문제의 저작권은 해커랭크에 있으며 문제를 풀어보시려면 아래 링크를 클릭해주세요 🙂

 

Challenges | HackerRank

Print the total number of challenges created by hackers.

www.hackerrank.com

 

 

 

문제 설명

더보기

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.

 

🎙️줄리아는 학생들에게 코딩 문제를 만들어오도록 했다. hacker_id, name, 그리고 각 학생의 challenge 총 횟수를 출력하는 쿼리를 작성하라. challenge 총 횟수의 내림차순으로 정렬하고 만약 같은 횟수가 있다면 hacker_id 순으로 정렬하라. 만약 challenge 총 횟수가 같고 횟수가 가장 큰 횟수보다는 작다면 결과에서 그 학생을 제외해라.

 

 

문제 풀이

hacker_id name, challenge 총 횟수만 출력하면 될 거 같지만 조건이 꽤나 까다롭다,,

challenge 총 횟수에 따라 조건이 달라진다.

중복된 challenge_num (challenge 총 횟수) < max(challenge_num) 일 경우 행 제외

중복된 challenge_num (challenge 총 횟수) = max(challenge_num) 일 경우, hacker_id 순으로 정렬

 

 

따라서 고려해야 할 조건은

 

1. hacker_id 별 name, challenge 수 출력 

2. challenge_num 중복값 처리  :

challenge_num < max(challenge_num) -> unique한 challenge_num 찾기

challenge_num = max(challenge_num) -> hacker_id 순으로 정렬

 

 

 

 

문제 풀이 과정

1. hacker_id 별 name, challenge 수 출력

WITH count_num AS (SELECT c.hacker_id, h.name, COUNT(c.challenge_id) AS challenge_num
                   FROM Challenges AS c
                   INNER JOIN Hackers AS h
                   ON c.hacker_id = h.hacker_id
                   GROUP BY c.hacker_id, h.name)

 

challenge_num으로 비교해야 할 것들이 많아서 with문을 통해 가상의 테이블을 만들어 조회하기 쉽도록 하였다.

 

 

 

2. max(challenge_num)인 행 조회, hacker_id 순으로 정렬

WITH count_num AS (SELECT c.hacker_id, h.name, COUNT(c.challenge_id) AS challenge_num
                   FROM Challenges AS c
                   INNER JOIN Hackers AS h
                   ON c.hacker_id = h.hacker_id
                   GROUP BY c.hacker_id, h.name)
                                    
SELECT hacker_id,
       name,
       challenge_num
FROM count_num
WHERE challenge_num = (select max(challenge_num) from count_num)
ORDER BY  hacker_id

 

 

 

3. unique한 challenge_num 찾기

WITH count_num AS (SELECT c.hacker_id, h.name, COUNT(c.challenge_id) AS challenge_num
                   FROM Challenges AS c
                   INNER JOIN Hackers AS h
                   ON c.hacker_id = h.hacker_id
                   GROUP BY c.hacker_id, h.name)
                                    
SELECT hacker_id,
       name,
       challenge_num
FROM count_num
WHERE challenge_num = (select max(challenge_num) from count_num)
OR challenge_num IN (select challenge_num from count_num 
                     group by challenge_num 
                     having count(*) = 1)
ORDER BY challenge_num DESC, hacker_id

 

 

 

 

최종 답안

WITH count_num AS (SELECT c.hacker_id, h.name, COUNT(c.challenge_id) AS challenge_num
                   FROM Challenges AS c
                   INNER JOIN Hackers AS h
                   ON c.hacker_id = h.hacker_id
                   GROUP BY c.hacker_id, h.name)
                                    
SELECT hacker_id,
       name,
       challenge_num
FROM count_num
WHERE challenge_num = (select max(challenge_num) from count_num)
OR challenge_num IN (select challenge_num from count_num 
                     group by challenge_num 
                     having count(*) = 1)
ORDER BY challenge_num DESC, hacker_id