Intersection command in SQL Server

This function is used to returns distinct values that are returned by both the query on the left and right sides of the INTERSECT operand. For intersection command, below basic rules must be followed.
  1. The number and the order of the columns must be the same in all queries.
  2. The data types must be compatible.
 Syntax:-

{ query_specification }
 INTERSECT
{ query_specification }

In this article, we are going to use the following table for the examples:-

CREATE TABLE [dbo]. [Employee](
[Empid] [Int] IDENTITY (1, 1) NOT NULL Primary key,
[EmpNumber] [nvarchar](50) NOT NULL,
[EmpFirstName] [nvarchar](150) NOT NULL,
[EmpLAStName] [nvarchar](150) NULL,
[EmpEmail] [nvarchar](150) NULL,
[Salary] [int] NULL
)

We insert following data into the above tables:-

Insert Into Employee (EmpNumber,EmpFirstName,EmpLAStName,EmpEmail,salary)
Values(‘A001′,’Vivek’,’Johari’,’samir@abcmail.com’,200000)

Insert Into Employee (EmpNumber,EmpFirstName,EmpLAStName,EmpEmail,salary)
Values(‘A002′,’Amit’,’Kumar’,’amit@abcmail.com’,100000)

Insert Into Employee (EmpNumber,EmpFirstName,EmpLAStName,EmpEmail,salary)
Values(‘A003′,’Neha’,’Sharma’,’neha@abcmail.com’,300000)

Insert Into Employee (EmpNumber,EmpFirstName,EmpLAStName,EmpEmail,salary)
Values(‘A004′,’Chandra’,’Singh’,’vivek@abcmail.com’,320000)

Insert Into Employee (EmpNumber,EmpFirstName,EmpLAStName,EmpEmail,salary)
Values(‘A005′,’Avinash’, ‘Dubey’,’avinash@abcmail.com’,250000)

Example

If we execute the below queries individually, we get the following result:-

Select Empnumber from employee where salary>200000


Result

A003
A004
A005

Select Empnumber from employee where salary between 100000 and 320000

Result

A001
A002
A003
A004
A005


Now if we use Intersection command between these queries, we get the following result.

Select Empnumber from employee where salary>200000
 INTERSECT
Select Empnumber from employee where salary between 100000 and 320000
Result

A003
A004
A005

Above example shows that the Intersection command will return only those distinct records which are return by both the queries(left side query as well as right side query).

About vivekjohari

Database Consultant with more than 11.5 years of experience in database designing & programming and DBA related activities.  Had good experience on different databases like SQL Server, MySQL & Oracle, Azure SQL &  Big Data.
This entry was posted in SQL Basic Concepts, SQL Server and tagged . Bookmark the permalink.

31 Responses to Intersection command in SQL Server

  1. Pingback: Creative Agency in Orlando

  2. Pingback: Dentists in Reno

  3. Pingback: Dentist in Dallas Texas

  4. Pingback: Torch search onion

  5. Pingback: สินเชื่อ ชัยภูมิ

  6. Pingback: engagement rings

  7. Pingback: โกงชักดาบ

  8. Pingback: Pre-Market Stocks Gold Silver Price Update Today New Futures MKT for Gold

  9. Pingback: https://internetnews.net.pl/wybierz-odpowiednie-spinki-koszuli-lub-garnituru/

  10. Pingback: Dentist in Victoria texas

  11. Pingback: Dream Market

  12. Pingback: Darknet Empire Market

  13. Pingback: w88

  14. Pingback: Digital Income System

  15. Pingback: ratucapsa

  16. Pingback: santali video gana song

  17. Pingback: www.senangpokerq.site

  18. Pingback: elang qq

  19. Pingback: www.1lapakqq.site

  20. Pingback: Eddie Frenay

  21. Pingback: Repo Expert Predicts Markets To Crash by Year End

  22. Pingback: qiuqiu99

  23. Pingback: raja capsa

  24. Pingback: mainqq

  25. Pingback: social

  26. Pingback: truck tailgate vinyl graphics

  27. Pingback: celebs nsfw

  28. Pingback: thevelocity-grp.com

  29. Pingback: engagement selfie frame

  30. Pingback: duratrans printing

  31. Pingback: fowling board decals

Leave a Reply