Home » SQL INTERSECT

SQL INTERSECT

by Online Tutorials Library

SQL INTERSECT

The INTERSECT is an operator in Structured Query Language that combines the rows of two SELECT statements and returns only those rows from the first SELECT statement, which are the same as the rows of the second SELECT statement.

In simple words, we can say that this operator shows common rows from both the SELECT statement.

This operator does not work in the MySQL database.



Syntax of INTERSECT operator:

The data type and the number of fields must be the same for every SELECT statement connected with the INTERSECT SQL operator.

Example of INTERSECT operator in SQL

To understand the example of INTERSECT operator, we have to create the two different tables Old_Faculty_Info and New_Faculty_Info in SQL and insert the records of IIT Faculty in both tables.

The following CREATE statement creates the Old_Faculty_Info table in the IIT_College Database:



The following INSERT queries insert the records of Faculties in the Old_Faculty_Info table:

The following SQL statement displays the records of the Old_Faculty_Info table on the screen:

 

Faculty_Id Faculty_First_Name Faculty_Last_Name Faculty_Dept_Id Faculty_Joining_Date Faculty_City Faculty_Salary
1001 Arush Sharma 4001 2020-01-02 Delhi 20000
1002 Bulbul Roy 4002 2019-12-31 Delhi 38000
1004 Saurabh Roy 4001 2020-10-10 Mumbai 45000
1005 Shivani Singhania 4001 2019-07-15 Kolkata 42000
1006 Avinash Sharma 4002 2019-11-11 Delhi 28000
1007 Shyam Besas 4003 2021-06-21 Lucknow 35000

The following CREATE statement creates the New_Faculty_Info table in theIIT_College Database:



The following INSERT queries insert the records of Faculties in the New_Faculty_Info table:

Step 4: View the Inserted Data

The following SELECT statement displays the data of the New_Faculty_Info table.

 

Faculty_Id Faculty_First_Name Faculty_Last_Name Faculty_Dept_Id Faculty_Joining_Date Faculty_City Faculty_Salary
1010 Ankush Roy 4004 2018-10-02 Delhi 25000
1001 Arush Sharma 4001 2020-01-02 Delhi 20000
1009 Raj Singhania 4005 2021-05-10 Noida 40000
1005 Shivani Singhania 4001 2019-07-15 Kolkata 42000
1008 Avinabh Chetya 4002 2018-11-11 Banglore 22000
1007 Shyam Besas 4003 2021-06-21 Lucknow 35000

The following query shows the common records of Faculty from both tables:



Output:

Faculty_Id Faculty_First_Name Faculty_Last_Name Faculty_Dept_Id Faculty_Joining_Date Faculty_City Faculty_Salary
1001 Arush Sharma 4001 2020-01-02 Delhi 20000
1005 Shivani Singhania 4001 2019-07-15 Kolkata 42000
1007 Shyam Besas 4003 2021-06-21 Lucknow 35000

INTERSECT operator with WHERE clause

The SQL WHERE clause can also be used with the INTERSECT operator for filtering the records from one or both tables.

Syntax of INTERSECT with WHERE clause

Example of INTERSECT with WHERE Clause

The following query shows those records of Faculties from the above tables whose salary is greater than and equal to 35000:



Output:

Faculty_Id Faculty_First_Name Faculty_Last_Name Faculty_Dept_Id Faculty_Joining_Date Faculty_City Faculty_Salary
1005 Shivani Singhania 4001 2019-07-15 Kolkata 42000
1007 Shyam Besas 4003 2021-06-21 Lucknow 35000

INTERSECT operator with ORDER BY clause

The SQL ORDER BY clause can also be used with the INTERSECT operator to show the records based on the specified group.

The Syntax of INTERSECT operator with ORDER BY clause is shown in the following block:

Example

To understand the example of Intersect operator with Order By clause, we have to create the two tables in SQL.



The following CREATE statement creates the Old_Employee_Info table in the IT_Company database:

The following INSERT queries insert the records of employees in the Old_Employee_Info table:

The following query shows the data of the Old_Employee_Info table.

 



Employee_ID Employee_Name Employee_Gender Employee_Age Employee_Saalary
1001 Arush Male 18 35000
1002 Bulbul Female 18 42000
1004 Saurabh Male 20 45000
1005 Shivani Female 25 28000
1006 Avinash Male 22 38000
1007 Shyam Male 18 20000

The following CREATE statement creates the New_Employee_Info table in the IT_Company database:

The following INSERT queries insert the records of employees in the New_Employee_Info table:

The following query shows the data of the New_Employee_Info table.

 



Employee_ID Employee_Name Employee_Gender Employee_Age Employee_Saalary
1007 Anuj Male 22 49000
1010 Saket Male 29 69000
1005 Shivani Female 25 28000
1006 Avinash Male 22 38000
1009 Female Male 18 25000

The following query shows the record of common employees from both the tables in descending order:

 

Employee_ID Employee_Name Employee_Gender Employee_Age Employee_Saalary
1006 Avinash Male 22 38000
1005 Shivani Female 25 28000

 

You may also like