Database/SQL Server

SQL Server - Except 연산자(operator)

청렴결백한 만능 재주꾼 2020. 12. 11. 06:14
반응형

EXCEPT 연산자는 오른쪽 쿼리 결과에 없는 왼쪽 쿼리에서만 있는 고유한 행을 리턴한다.
EXCEPT operator returns unique rows from the left query that aren't in the right query's results.

 

 - SQL Server 2005에 소개되었다.
Introduced in SQL Server 2005

 - 열의 개수와 순서는 두 쿼리에서 동일해야 한다.
The number and the order of the columns must be the same in both the queries

--안되는 경우 
Select Name, Id, Gender from TableA
Except
Select Name, Id from TableB

--컬럼의 숫자와 순서가 동일해야 한다.
Selet Name, Id from TableA
Except
Select Name, Id from TableB

 - 데이터 유형은 동일하거나 호환 가능해야 한다. The data types must be same or compatible

 - 오라클에 Minus operator와 유사하다. This is similar to minus operator in oracle

- 테이블 한개로도 사용이 가능하다 You can also use Except operator on a single table

- 오른쪽 쿼리가 끝난 후 Order By 절을 쓸 수 있다. Order By clause should be used only once after the right query

 

 

 

EXCEPT 와 NOT IN 의 차이 Difference between EXCEPT and NOT IN

EXCEPT 연산자는 오른쪽 쿼리 결과에 없는 왼쪽 쿼리의 모든 행을 리턴한다. NOT IN도 마찬가지이다.

EXCEPT operator returns all the rows from the left query that aren't in the right query's results. NOT IN operator also does the same.

EXCEPT은 중복을 필터하고 오른쪽 쿼리 결과에 없는 왼쪽쿼리에서 DISTINCT한 행만 리턴한다. 여기서 NOT IN은 중복 항목을 필터링 하지 않는다.
EXCEPT filters duplicates and returns only DISTINCT rows from the left query that aren't in the right query's results, where as NOT IN does not filter the duplicates

EXCEPT 연산자는 양 쪽의 쿼리에서 같은 개수의 열을 예상한다. 근데 NOT IN은 양 쪽의 쿼리가 아닌 외부 쿼리와 서브쿼리의 단일 열과 비교를 한다. 
EXCEPT operator expects the same number of columns in both the queries, where as NOT IN, compares a single column from the outer query with a single column from the sub-query.

반응형