중첩(일반) 서브쿼리
# 중첩 서브쿼리 실습
# 문동은의 나이보다 나이가 많은 모든 데이터 반환하기
select *
from basic.theglory
where 나이 > (select 나이 from basic.theglory where 이름='문동은')
;
스칼라 서브쿼리
# 스칼라 서브쿼리 실습
# theglory 의 이름과 theglory2 테이블의 이름이 일치하는 경우를 count 하여
# same_name_cnt 컬럼으로 반환
# theglory 의 이름과 theglory 테이블의 이름이 일치하는 경우의 결제금액을 sum 하여
# same_name_sumamount 컬럼으로 반환
select 이름
, 나이
, (select count(*) from theglory2 where theglory2.이름=theglory.이름) as same_name_cnt
, (select sum(결제금액) from theglory2 where theglory2.이름=theglory.이름)as same_name_sumamount
from basic.theglory
;
인라인 뷰(가장 많이 사용)
# 인라인뷰 서브쿼리 실습
# 나이가 33세 이상인 모든 데이터 중 나이와 직업 컬럼 반환하기
select x.나이, x.직업
from( select *
from basic.theglory
where 나이>=33
)as x