[SQL] Contest Leaderboard(hackerrank)

건너별·2022년 4월 17일
0

SQL

목록 보기
14/14

문제 풀기

  • group by 와 서브쿼리 활용.
  • 대회에서 유저들이 여러 컨테스트에서 제출한 최고점수의 합계를 출력하는 문제.
  • 추가 조건(0이상인 것만 출력)을 걸어주어야 해서 좀 더 어려웠음.
/*
Enter your query here.
*/
select hacker_id, name, sum(score) as total_score
from(
    select s.hacker_id,h.name, challenge_id,  max(score) as score 
    from submissions s inner join hackers h on h.hacker_id=s.hacker_id  
    group by hacker_id,h.name, challenge_id
    )
as t
group by hacker_id, name
having total_score>0
order by total_score desc, hacker_id asc;

기억해야 할 것🧐

  • group by 를 여러번 해야 한다면 전체 select~ from 절을 하나의 table로 놓고, mysql에서는 as t 처럼 이름도 명명해주어야 오류가 나지 않는다.
  • group by 를 쓴 상태에서 where 절을 동시에 쓰는 건 불가능한 듯 하다. 한 번 더 쿼리로 감싸주었다가, having을 쓰면 된다는 것을 깨닫고 아차 싶어 다시 수정했다.
profile
romantic ai developer

0개의 댓글