[프로그래머스] 대장균들의 자식의 수 구하기

yenpkr·2025년 3월 4일
0

sql

목록 보기
45/91

문제

제출

SELECT a.id,ifnull(child_count,0) CHILD_COUNT
from ecoli_data a
left join (select parent_id,count(*) child_count
            from ecoli_data
            where parent_id is not null
            group by parent_id) b
on a.id = b.parent_id
order by 1 asc

또 다른 답

select a.id,count(b.parent_id) CHILD_COUNT
from ecoli_data a
left join ecoli_data b
on a.id = b.parent_id
group by 1
order by 1 asc

굳이 join에 서브쿼리 쓸 필요가 없었다.

0개의 댓글