본문 바로가기

Algorithm/MySQL

[해커랭크(HackerRank)] Top Competitors (MySQL)

문제

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개 이상의 과제에서 만점을 획득한 해커의 이름과 각 hacker_id를 출력
  • 만점을 받은 총 과제 수를 기준으로 내림차순으로 정렬
  • 둘 이상의 해커가 동일한 수의 과제에서 만점을 받은 경우, 오름차순 hacker_id를 기준으로 정렬

  • Hackers : 전체 hackers의 정보
  • Difficulty : 문제 난이도 별 Full Score 정보
  • Challenges : 문제와 문제를 제작한 Hacker 정보, 문제의 난이도 정보
  • Submissions : 문제를 제출한 사람의 정보, 제출시 score 정보

코드

0. 문제를 이해하는데 너무 오래 걸렸다.. 눈물이 나요!!! 문제 풀이를 잘못했어요..

1. submission 을 기준으로 데이터를 뽑아오면 되기 때문에 submission을 기준으로 뽑아오기

2. 먼저 submission 테이블의 제출한 사람을 알아보기 위해 hackers 테이블과 조인

select *
from submissions a 
     inner join hackers b on a.hacker_id = b.hacker_id

3. submission 문제 아이디를 알아야 난이도를 알 수 있기 때문에 challenges 테이블과 조인

select *
from submissions a 
     inner join hackers b on a.hacker_id = b.hacker_id
     ineer join challenges c on a.challenge_id = c.challenge_id

 4. difficulty 테이블과 challenges 테이블 조인하여 난이도 별 만점 점수 확인하기

select a.hacker_id, b.hacker_id, b.name, a.challenge_id, c.difficulty_level, d.score
from submissions a 
     inner join hackers b on a.hacker_id = b.hacker_id
     inner join challenges c on a.challenge_id = c.challenge_id
     inner join difficulty d on c.difficulty_level = d.difficulty_level

5. difficulty의 난이도 별 만점 점수와 submission에서 제출한 사람들이 받은 점수 비교! 만점일 때만 데이터 출력되야하니까!

select b.hacker_id, b.name, count(a.challenge_id)
from submissions a 
     inner join hackers b on a.hacker_id = b.hacker_id
     inner join challenges c on a.challenge_id = c.challenge_id
     inner join difficulty d on c.difficulty_level = d.difficulty_level
where a.score = d.score
group by b.hacker_id, b.name

6. count수가 2개 이상인 경우 총 과제 수를 기준으로 내림차순으로 정렬, 둘 이상의 해커가 동일한 수의 과제에서 만점을 받은 경우, 오름차순 hacker_id를 기준으로 정렬

select b.hacker_id, b.name
from submissions a 
     inner join hackers b on a.hacker_id = b.hacker_id
     inner join challenges c on a.challenge_id = c.challenge_id
     inner join difficulty d on c.difficulty_level = d.difficulty_level
where a.score = d.score
group by b.hacker_id, b.name
having count(a.submission_id) >= 2
order by count(a.submission_id) desc, b.hacker_id

출처

반응형