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_IdFaculty_First_NameFaculty_Last_NameFaculty_Dept_IdFaculty_Joining_DateFaculty_CityFaculty_Salary
1001ArushSharma40012020-01-02Delhi20000
1002BulbulRoy40022019-12-31Delhi38000
1004SaurabhRoy40012020-10-10Mumbai45000
1005ShivaniSinghania40012019-07-15Kolkata42000
1006AvinashSharma40022019-11-11Delhi28000
1007ShyamBesas40032021-06-21Lucknow35000

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_IdFaculty_First_NameFaculty_Last_NameFaculty_Dept_IdFaculty_Joining_DateFaculty_CityFaculty_Salary
1010AnkushRoy40042018-10-02Delhi25000
1001ArushSharma40012020-01-02Delhi20000
1009RajSinghania40052021-05-10Noida40000
1005ShivaniSinghania40012019-07-15Kolkata42000
1008AvinabhChetya40022018-11-11Banglore22000
1007ShyamBesas40032021-06-21Lucknow35000

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

Output:

Faculty_IdFaculty_First_NameFaculty_Last_NameFaculty_Dept_IdFaculty_Joining_DateFaculty_CityFaculty_Salary
1001ArushSharma40012020-01-02Delhi20000
1005ShivaniSinghania40012019-07-15Kolkata42000
1007ShyamBesas40032021-06-21Lucknow35000

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_IdFaculty_First_NameFaculty_Last_NameFaculty_Dept_IdFaculty_Joining_DateFaculty_CityFaculty_Salary
1005ShivaniSinghania40012019-07-15Kolkata42000
1007ShyamBesas40032021-06-21Lucknow35000

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_IDEmployee_NameEmployee_GenderEmployee_AgeEmployee_Saalary
1001ArushMale1835000
1002BulbulFemale1842000
1004SaurabhMale2045000
1005ShivaniFemale2528000
1006AvinashMale2238000
1007ShyamMale1820000

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_IDEmployee_NameEmployee_GenderEmployee_AgeEmployee_Saalary
1007AnujMale2249000
1010SaketMale2969000
1005ShivaniFemale2528000
1006AvinashMale2238000
1009FemaleMale1825000

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


Employee_IDEmployee_NameEmployee_GenderEmployee_AgeEmployee_Saalary
1006AvinashMale2238000
1005ShivaniFemale2528000

Next Topic#




Latest Courses