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.
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
Createtable dbo.Customer_AS (
CustomerId Int ,
CustomerName VARCHAR(100),
StreetAddress VARCHAr(100),
City VARCHAR(100),
StateCHAR(2))
go
Createtable dbo.Customer_NA (
CustomerId Int ,
CustomerName VARCHAR(100),
StreetAddress VARCHAr(100),
City VARCHAR(100),
StateCHAR(2))
go
--Insert couple of Records in Sample Table
Insertinto dbo.Customer_AS
Select 1,'Aamir shahzad','Test Street Address','Charlotte','NC'
Unionall
Select 2,'M Raza','Test Street Address','Charlotte','NC'
unionall
Select 1,'Aamir shahzad','Test Street Address','Charlotte','NC'
Insertinto 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