SQL

where 절의 비교연산, between, in, like, 논리연산

content0474 2024. 8. 27. 08:54

비교연산

= 같다

<> 같지않다

> 크다

< 작다

>= 크거나 같다

<= 작거나 같다

 

select *

from customers

where age >=21

:customers 테이블에서 나이가 21세 이상인 데이터를 조회

 

Between

between A and B : A와 B 사이

select *

from customers

where age between 24 and 27

:customers 테이블에서 나이가 24-27세 사이인 데이터를 조회

 

In

in ( ) : ( )안의 조건에 해당하는 것들

select *

from customers

where age in (21, 24)

:customers 테이블에서 나이가 21세와 25세인 데이터를 조회

 

select *

from customers

where name in ('김도윤', '박채아')

:customers 테이블에서 이름이 김도윤인 고객과 박채아인 데이터를 조회

 

Like

like ~같은 ~와 비슷한 (똑같지는 않지만 비슷한)

% 이 자리에 무엇이 들어가도 상관없다는 뜻

select *

from customers

where name like '%윤'

:customers 테이블에서 이름 끝글자가 윤인 데이터를 조회

 

select *

from customers

where name like '박%'

:customers 테이블에서 이름 앞글자가 박인 데이터를 조회

 

select *

from food_orders

where restaurant_name like '%blue%'

:food_orders 테이블에서 식당이름 중간에 blue가 들어가는 데이터를 조회

 

논리연산

한번에 여러 조건들을 쓸 때

AND 그리고

OR 또는

NOT 아닌

 

select *

from food_orders

where price>=30000

or cuisine_type='korean'

: food_orders 테이블에서 가격이 3만원 이상이거나 또는 한식인 데이터를 조회

 

select *

from customers

where age between 24 and 27

and gender='male'

:customers 테이블에서 24세-27세 사이의 남성 데이터를 조회

 

select *

from customers

where age between 24 and 27

and not gender='male'

:customers 테이블에서 24세-27세 사이의 여성 데이터를 조회

 

select *

from customers

where age between 24 and 27

and gender<>'female'

:customers 테이블에서 24세-27세 사이의 남성 데이터를 조회

 

select email

from customers

where age between 24 and 27

and gender='male'

:customers 테이블에서 24세-27세 사이의 남성의 이메일 주소만 조회

'SQL' 카테고리의 다른 글

if, case  (0) 2024.08.29
replace, substring, concat  (1) 2024.08.28
group by, order by  (1) 2024.08.28
기본 연산, 최대/최소, 개수 구하기  (0) 2024.08.27
select, from  (0) 2024.08.27