줄리아는 학생들에게 코딩 챌린지들을 만들라고 지시했다.
- hacker_id, name, 각 학생들이 만든 코딩 챌린지 갯수를 출력하는 쿼리를 작성해라.
- 총 갯수에 따라 내림차순으로 정렬
- 갯수가 같다면, hacker_id 기준으로 정렬
- 한 명 이상의 학생이 같은 갯수의 챌린지를 작성하고, 갯수가 maximum number of challenges created 보다 작다면, 해당 학생들을 제외하라.
필요한 서브 테이블
having count(*) in
이렇게 쓸 수 있다. 필터링 조건에 >, <, = 뿐만 아니라 IN
도 쓸 수 있음. select h.hacker_id, h.name, count(*)
from Hackers h
join Challenges c on h.hacker_id = c.hacker_id
group by h.hacker_id, h.name
having count(*) in (select counted from (
select count(*) as counted
from Hackers h
join Challenges c on h.hacker_id = c.hacker_id
group by h.hacker_id)counted_table
group by counted
having count(*) = 1)
or chal_count = (select max(counted) from (
select count(*) as counted
from Hackers h
join Challenges c on h.hacker_id = c.hacker_id
group by h.hacker_id)counted_table)
order by chal_count DESC, h.hacker_id
select h.hacker_id, h.name, count(*) as chal_count
from Hackers h
join Challenges c on h.hacker_id = c.hacker_id
group by h.hacker_id, h.name
having chal_count in (select counted from (
select count(*) as counted
from Hackers h
join Challenges c on h.hacker_id = c.hacker_id
group by h.hacker_id)counted_table
group by counted
having count(counted) = 1)
or chal_count = (select max(counted) from (
select count(*) as counted
from Hackers h
join Challenges c on h.hacker_id = c.hacker_id
group by h.hacker_id)counted_table)
order by chal_count DESC, h.hacker_id
chal_count로 지정하여 having에 넣었더니 잘 조회된다.