What is Intersect Operator in SQL Server - SQL Server Tutorial / TSQL Tutorial

The Intersect Operator returns us distinct matching records from two or more Select queries ( Result Sets).

For Intersect Operator to work, the Number of columns should be same in all Select queries and data type should match.

--drop table dbo.Customer_AS
--drop table dbo.Customer_NA
Create table dbo.Customer_AS ( 
CustomerId Int ,
CustomerName VARCHAR(100),
StreetAddress VARCHAr(100),
City VARCHAR(100),
State CHAR(2))
go

Create table dbo.Customer_NA ( 
CustomerId Int ,
CustomerName VARCHAR(100),
StreetAddress VARCHAr(100),
City VARCHAR(100),
State CHAR(2))
go


--Insert couple of Records in Sample Table
Insert into dbo.Customer_AS
Select 1,'Aamir shahzad','Test Street Address','Charlotte','NC'
Union all
Select 2,'M Raza','Test Street Address','Charlotte','NC'
union all
Select 1,'Aamir shahzad','Test Street Address','Charlotte','NC'



Insert into dbo.Customer_NA
Select 1,'Aamir shahzad','Test Street Address','Charlotte','NC'
Union 
Select 3,'John Smith','Test Street Address','New York City','NY'



--This will return us only one distinct matching record
Select * from dbo.Customer_AS
Intersect
Select * from dbo.Customer_NA





Intersect Operator in SQL Server





Watch the video for Detail Demo of Intersect Operator in SQL Server