[SQL Runday] HackerRank - Top Competitors

줄리아는 방금 코딩 대회 진행을 마쳤고, 리더보드를 구성하는데 도움이 필요합니다! 각각의 hacker_id에 따라 이름과 1개 이상의 챌린지를 만점에 성공한 해커들의 성적을 출력하는 쿼리를 작성하세요.

  • 해커가 만점(full score)맞은 총 챌린지 갯수를 기준으로 내림차순으로 정렬하세요.
  • 1명 이상의 해커가 같은 갯수의 챌린지를 성공했다면, hacker_id 기준 오름차순으로 정렬하세요.

Key Points

  • where, group by, having 서순!!!
  • 다 맞게 썼는데 자꾸 에러나서 뭔가 했더니, 서순을 계속 틀리고 있었다.
  • 외울것. where로 필터링하고, group by로 count세주고, group by 한 칼럼 조건 걸기 위해선 having!!
    처음 join만 써서 다 구하기
select s.submission_id, s.hacker_id, s.challenge_id, s.score,
        h.name, c.difficulty_level, d.score
from submissions s
join hackers h on s.hacker_id = h.hacker_id
join challenges c on c.challenge_id = s.challenge_id
join difficulty d on d.difficulty_level = c.difficulty_level
where s.score = d.score

잘 구했으므로, 여기서 이제 count(*)로 해커별로 만점 맞은 행 세주고, where-group by- having 순으로 필터링 걸고, Order by로 정렬할 것.

최종 쿼리

select h.hacker_id, h.name
from submissions s
join hackers h on s.hacker_id = h.hacker_id
join challenges c on c.challenge_id = s.challenge_id
join difficulty d on d.difficulty_level = c.difficulty_level
where s.score = d.score
group by h.name, h.hacker_id
having count(*) > 1
order by count(*) DESC, h.hacker_id

좋은 웹페이지 즐겨찾기