이 문제 하나 푸는데 거의 2시간이 걸렸다.
처음에는 쉽게 접근하다가 문제의 조건인 아래 2가지를 해결하는데 정말 애먹었다.
처음에는
max(count(challenge_id))
와 같이 집계함수 안에 집계함수를 넣는 정말 신박하고 멍청한 방법을 떠올렸다.
이후 서브쿼리를 많이 써서 해결하자는 전략으로 하나씩 해결해보았다.
select total
from (select hacker_id, count(challenge_id) as total
from challenges as sub1
group by hacker_id) as sub2
group by sub2.total
having count(sub2.total)=1
select max(C4.total)
from (select hacker_id, count(challenge_id) as total
from challenges as sub3
group by hacker_id) as sub4
select H.hacker_id, H.name, count(C.challenge_id) as total
from hackers as H
join challenges as C
on H.hacker_id = C.hacker_id
group by H.hacker_id, H.name
having total in (select C2.total
from (select hacker_id, count(challenge_id) as total
from challenges as C1
group by hacker_id) as C2
group by C2.total
having count(C2.total)=1)
or total = (select max(C4.total)
from (select hacker_id, count(challenge_id) as total
from challenges as C3
group by hacker_id) as C4)
order by total desc, H.hacker_id