Learning Objective
The objective of this SQL Server tutorial is to teach you how to use INTERSECT operator to extract common records from a combination.
What is INTERSECT in SQL Server?
The INTERSECT operator is used to extract records which are common in the resultset of two or more SELECT queries distinctly (i.e. with the records represented only once in the resultset).
Prerequisites: The prerequisite for a successful INTERSECT is that the query result sets should have the same number of columns in the same order and with the same or similar datatype (i.e. the column datatypes should be either exactly same or it should be possible to convert one datatype into another without casting error).
The INTERSECT operation can be pictorially represented as below.
Operation
As discussed above the INTERSECT operator can only be used with SELECT queries and the query result sets should be of same size.
SQL Server INTERSECT Syntax
The basic syntax of the INTERSECT operator is given below.
INTERSECT syntax
SELECT_QUERY_1 INTERSECT SELECT_QUERY_2;
In this syntax,
- SELECT_QUERY – SQL query which selects rows of information (i.e. records or tuples) from a table. It can be a simple or complex query with conditions.
- INTERSECT – SQL keyword to combine query result sets of two or more queries and extract the common records distinctly.
SQL Server INTERSECT Examples
Let us consider an example to understand its practical usage and implications. Suppose we have two tables – a table called persons and another table called customers. The persons table contains prospect information and the customers table contains customer information. Data wise the customers table is a subset of the persons table with the persons table containing 10 records and the customers table containing 8 records. The sample tables are shown below. Please note that the column names are different in the tables and that the persons table is one column short. But however, it does not violate the prerequisites since they relate to the resultset and not to the table. The tables can have dissimilar number of columns and column names.
first_name | last_name | pers_city |
Herbert | Einstein | New York |
Jackie | Frost | Seattle |
Stephen | George | Washington |
James | Mare | New York |
Angel | Reeves | Dallas |
Selena | Spears | Detroit |
Sammy | Louise | Dallas |
Brandon | Powell | Seattle |
Kim | Fox | Washington |
Val | Costner | New York |
fname | lname | city | contact |
Stephen | George | Washington | 16553120210 |
James | Mare | New York | 16277212992 |
Angela | Crawford | Washington | 16633775159 |
Selena | Spears | Detroit | 16616165325 |
Patty | Campbell | Detroit | 16107575525 |
Brandon | Powell | Seattle | 16101110778 |
Kim | Fox | Washington | 16529929936 |
Edith | Poe | Seattle | 16767335231 |
1) SQL Server INTERSECT – INTERSECT example
We will do an INTERSECT between the SELECT queries retuning the first and last names from the two tables.
SELECT first_name, last_name FROM persons INTERSECT SELECT fname, lname FROM customers;
It will run successfully and generate the following resultset with the column names from the first table persons as the column heading.
first_name | last_name |
Brandon | Powell |
James | Mare |
Kim | Fox |
Selena | Spears |
Stephen | George |
Now let us try to run the following INTERSECT query with the participating SELECT queries returning all columns from the tables. As we can see below the query fails with error because of * trying to fetch all columns from the tables and the numbers of columns being different – 3 in the persons table and 4 in customers.
SELECT * FROM persons INTERSECT SELECT * FROM customers; Msg 205, Level 16, State 1, Server MY-HP245G6, Line 1 All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.