코딩테스트/HackerRank

[Basic Join] Top Competitors(Oracle)

봉두두 2022. 5. 3. 16:53
728x90
Level : SQL (Intermediate)

 

💡 Key note
1. Score가 100이 무조건 만점이 아님. 반드시 Difficulty table을 활용해야 함.
2. 만점을 2번 이상 받은 해커를 출력함. (문제에서 More than one challenge 부분)

 

-- hacker_id, name을 출력함
-- 이 때 하나 이상의 challenge에서 만점을 받은 hacker를 출력함
-- 이 때 만점 받은 challenge의 개수가 높은 순으로 출력함
-- 만점받은 개수가 동일한 해커에 대해서는 hacker_id에 대해 ascending ordering 함

select subquery.hacker_id, subquery.name
from (
    select sub.hacker_id, hac.name, count(*) cnt
    from submissions sub 
    left join hackers hac on sub.hacker_id = hac.hacker_id
    left join challenges chall on sub.challenge_id = chall.challenge_id
    left join difficulty dif on chall.difficulty_level = dif.difficulty_level
    where sub.score = dif.score
    group by sub.hacker_id, hac.name
     ) subquery
where subquery.cnt > 1
order by subquery.cnt desc, subquery.hacker_id asc;

 

728x90
728x90