제로베이스 SQL (Subquery)

ningbbang·2023년 5월 20일
0

Zerobase DS13

목록 보기
33/48

SQL Subquery

1) Scalar Subquery
2) Inline View
3) Nested Subquery

1. Scalar Subquery

select 절에 사용하고 결과는 하나의 column

select select, (select * from db where condition) 
from db
where condition

2. Inline View

from 절에 사용하는 subquery
메인쿼리에서는 인라인 뷰에서 조회한 column만 사용 가능

select select
from db, (select * from db where condition) 
where condition

3. Nested Subquery

where 절에 사용하는 subquery

  • single row : 하나의 행을 검색하는 서브쿼리
  • multiple row : 하나 이상의 행을 검색하는 서브쿼리
  • multiple column : 하나 이상의 열을 검색하는 서브쿼리

1) single row

select select
from db
where condition = (select * from db where condition)

2) multiple row - in

select select
from db
where condition in (select * from db where condition)

2) multiple row - exists

select select
from db
where condition exists (select * from db where condition)

3) multiple row - any

select select
from db
where condition = any (select * from db where condition)

4) multiple row - all

select select
from db
where condition = all (select * from db where condition)

5) multiple column
서브쿼리 내에 메인쿼리 컬럼이 같이 사용되는 경우

select column1, column2, ---
from db
where (colum1, column2) in (select column1, column2 from db where condition)
profile
HR Anaylist!

0개의 댓글