SQL Server / 연산자 / 비교 연산자, 논리 연산자
예제로 사용한 데이터베이스는 AdventureWorks이다.
목차
같다
- HumanResources.Employee 테이블에서 BusinessEntityID가 10인 데이터를 출력한다.
select * from HumanResources.Employee where BusinessEntityID = 10 ;
같지 않다
- HumanResources.Employee 테이블에서 BusinessEntityID가 10보다 작거나 큰, 즉 같지 않은 데이터를 출력한다.
select * from HumanResources.Employee where BusinessEntityID <> 10 ;
- <> 대신 !=를 사용해도 된다.
select * from HumanResources.Employee where BusinessEntityID != 10 ;
크다
- HumanResources.Employee 테이블에서 BusinessEntityID가 10보다 큰 데이터를 출력한다.
select * from HumanResources.Employee where BusinessEntityID > 10 ;
크거나 같다 / 작지 않다
- HumanResources.Employee 테이블에서 BusinessEntityID가 10보다 크거나 같은 데이터를 출력한다.
select * from HumanResources.Employee where BusinessEntityID >= 10 ;
- 크거나 같은 것은 작지 않은 것이므로 >= 대신 !<를 사용해도 된다.
select * from HumanResources.Employee where BusinessEntityID !< 10 ;
작다
- HumanResources.Employee 테이블에서 BusinessEntityID가 10보다 작은 데이터를 출력한다.
select * from HumanResources.Employee where BusinessEntityID < 10 ;
작거나 같다 / 크지 않다
- HumanResources.Employee 테이블에서 BusinessEntityID가 10보다 작거나 같은 데이터를 출력한다.
select * from HumanResources.Employee where BusinessEntityID <= 10 ;
- 작거나 같은 것은 크지 않은 것이므로 <= 대신 !>를 사용해도 된다.
select * from HumanResources.Employee where BusinessEntityID !> 10 ;
AND / OR
여러 조건에 대한 비교를 하고 싶다면 AND나 OR를 사용한다. AND는 모든 조건을 만족하는 것, OR는 적어도 하나를 만족하는 것을 찾는다.
- HumanResources.Employee 테이블에서 BusinessEntityID가 10보다 크거나 같고 20보다 작거나 같은 데이터를 출력한다.
select * from HumanResources.Employee where BusinessEntityID >= 10 and BusinessEntityID <= 20 ;
- HumanResources.Employee 테이블에서 BusinessEntityID가 10보다 작거나 20보다 큰 데이터를 출력한다.
select * from HumanResources.Employee where BusinessEntityID < 10 or BusinessEntityID > 20 ;
조건에 괄호가 있으면 괄호 안의 조건을 먼저 체크한다.
- HumanResources.Employee 테이블에서 BusinessEntityID가 10 이상 20 이하 또는 100인 데이터를 출력한다.
select * from HumanResources.Employee where ( BusinessEntityID >= 10 and BusinessEntityID <= 20 ) or BusinessEntityID = 100 ;
BETWEEN
BETWEEN으로 두 값 사이의 데이터를 추출할 수 있다.
- HumanResources.Employee 테이블에서 BusinessEntityID가 10보다 크거나 같고 20보다 작거나 같은 데이터를 출력한다.
select * from HumanResources.Employee where BusinessEntityID between 10 and 20 ;
- 즉, 아래와 같다.
select * from HumanResources.Employee where BusinessEntityID >= 10 and BusinessEntityID <= 20 ;
IN / NOT IN
IN으로 적어도 하나와 일치하는 데이터를 추출할 수 있다.
- HumanResources.Employee 테이블에서 BusinessEntityID가 10 또는 20 또는 30인 데이터를 출력한다.
select * from HumanResources.Employee where BusinessEntityID in ( 10, 20, 30 ) ;
- 이는 다음과 같이 할 수도 있다.
select * from HumanResources.Employee where BusinessEntityID = 10 or BusinessEntityID = 20 or BusinessEntityID = 30 ;
NOT IN으로 일치하지 않는 데이터를 추출할 수 있다.
- HumanResources.Employee 테이블에서 BusinessEntityID가 10이 아니고, 20이 아니고, 30이 아닌 데이터를 출력한다.
select * from HumanResources.Employee where BusinessEntityID not in ( 10, 20, 30 ) ;
- 이는 다음과 같이 할 수도 있다.
select * from HumanResources.Employee where BusinessEntityID <> 10 and BusinessEntityID <> 20 and BusinessEntityID <> 30 ;
데이터 형식이 문자열, 날짜인 경우
데이터 형식이 문자열 또는 날짜인 경우 사전식 배열순으로 대소를 정한다. 즉 A는 B보다 작다.
- HumanResources.Employee 테이블에서 HireDate가 2008-01-06보다 크거나 같은, 즉 2008-01-06 이후에 고용된 사람의 데이터를 출력한다.
select * from HumanResources.Employee where HireDate >= '2008-01-06' order by HireDate ;
- HumanResources.Employee 테이블에서 JobTitle이 HireDate가 buyer보다 작은, 즉 사전식으로 배열했을 때 buyer보다 앞에 있는 사람의 데이터를 출력한다.
select * from HumanResources.Employee where JobTitle < 'buyer' order by JobTitle ;