작성
·
245
0
답변 1
2
안녕하십니까,
네, 물론입니다. 인라인 서브쿼리 대신 with 절을 사용하셔도 무방합니다.
인라인 서브쿼리와 with절은 거의 동일합니다. 다만 with절은 인라인 서브쿼리의 약간(?)의 문제점을 대체하기 위해서 나온것인데, With 절의 집합을 SQL 전체에서 사용할 때는 해당 집합을 얼마든지 With 절에 as로 명시된 집합명으로 사용할 수 있는데 반해서 인라인 서브쿼리는 그렇지 못하다는 차이가 있습니다.
가령 with 절의 집합을 self 조인하면 아래와 같이 with 절의 as 집합명을 이용하여 쉽게 가능하지만
with
temp_01 as (
select product_id, sum(amount) as sum_amount
from nw.orders a
join nw.order_items b
on a.order_id = b.order_id
group by product_id
)
select * from temp_01 a, temp_01 b
where a.product_id = b.product_id;
인라인 서브쿼리의 경우는 해당 집합을 아래와 같이 중복해서 명시해 줘야 합니다.
select * from
(select product_id, sum(amount) as sum_amount
from nw.orders a
join nw.order_items b
on a.order_id = b.order_id
group by product_id
) a,
(select product_id, sum(amount) as sum_amount
from nw.orders a
join nw.order_items b
on a.order_id = b.order_id
group by product_id
) b
where a.product_id = b.product_id;
두 질문 모두 이해했습니다! 친절하게 설명해주셔서 감사드립니다! :)