[내일배움캠프 사전캠프] subquery, join

sleekstar·2025년 4월 25일

subquery

수학에서의 괄호와 비슷한 개념
서브쿼리는 여러 개 있을 수 있다. 여러 개의 서브쿼리가 있을 때는 가장 가운데에 있는 괄호 부분부터 읽으면 해석할 수 있음.

<학습하며 겪었던 에러>

select cuisine_type, 
total_quantity,
count_res
case when count_res>=5 and total_quantity>=30 then 0.005
when count_res>=5 and total_quantity<30 then 0.008
when count_res<5 and total_quantity>=30 then 0.01
when count_res<5 and total_quantity<30 then 0.02 end rate
from
(
select cuisine_type, 
sum(quantity) total_quantity,
count(distinct restaurant_name) count_res
from food_orders
group by 1
)

맞게 작성한 것 같고 캡쳐본과 비교해봐도 똑같은데 왜 오류가 날까...?

Join

수학에서의 집합과 비슷한 개념
필요한 데이터가 서로 다른 테이블에 있을 때, 공통된 칼럼을 기준으로 데이터를 묶을 수 있다.
left join은 A테이블과 B테이블을 묶을 때 B테이블에는 정보가 없더라도 A테이블 데이터는 모두 조회할 때 쓴다. A테이블에만 있고 B테이블에 없는 정보는 로 표시된다.

inner join은 공통된 정보가 있는 데이터만 불러온다.
<어떤 테이블에서 뽑느냐, 어떤 칼럼으로 묶느냐>가 핵심.

profile
기록용

0개의 댓글