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


2 comments:

  1. TODAY I GOT MY DESIRED XMAS LOAN AMOUNT $520,000.00 FROM A RELIABLE AND TRUSTED LOAN COMPANY. IF YOU NEED A LOAN NOW EMAIL CONTACT drbenjaminfinance@gmail.com

    Hello, I'm here to testify of how i got my loan from BENJAMIN LOAN FINANCE(drbenjaminfinance@gmail.com) I don't know if you are in need of an urgent loan to pay bills, start business or build a house, they offer all kinds of loan. So feel free to contact Dr. Benjamin Owen he holds all of the information about how to obtain money quickly and painlessly without cost/stress via Email: drbenjaminfinance@gmail.com

    Consider all your financial problems tackled and solved ASAP. Share this to help a soul right now THANKS.

    ReplyDelete
  2. For those who want an emergency loan, I offer you an affordable 3% interest rate. Rejected by multiple banks? Are you desperate for debt consolidation financing? Alternatively, you need financing for each track's business. Therefore, we are the best solution for your needs. We offer loans to interested clients without penalty. If you are interested. You can contact us via email {directaccesservice@gmail.com} or whatsapp @ +15754148400

    ReplyDelete