An essential tool for data manipulation is the SQL INTERSECT. In essence, INTERSECT joins two tables and yields a result set that contains the intersection (common records) of the two tables. We will study the fundamental syntax, applications, and sample INTERSECT examples on our example table in this post.
INTERSECT only returns the rows which are present in both the result sets (result sets from SELECT statements). It is used to find the common records – some real world scenarios where INTERSECT will be used are:
The basic way to find the intersection of two tables is to put INTERSECT between two SELECT statements. The above code is the basic syntax to find the intersection.
SELECT column1, column2, ...
FROM table1
INTERSECT
SELECT column1, column2, ...
FROM table2;
Let us now learn to create sample data:
CREATE TABLE Employees (
employee_id INT PRIMARY KEY,
name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2)
);
CREATE TABLE Contractors (
contractor_id INT PRIMARY KEY,
name VARCHAR(50),
department VARCHAR(50),
hourly_rate DECIMAL(10, 2)
);
Creation of necessary tables:
INSERT INTO Employees (employee_id, name, department, salary) VALUES
(1, 'Alice', 'HR', 60000),
(2, 'Bob', 'Engineering', 80000),
(3, 'Charlie', 'Marketing', 50000),
(4, 'David', 'Engineering', 75000);
INSERT INTO Contractors (contractor_id, name, department, hourly_rate) VALUES
(101, 'Eve', 'Engineering', 50),
(102, 'Frank', 'HR', 45),
(103, 'Grace', 'Engineering', 55),
(104, 'Charlie', 'Marketing', 60);
Inserting necessary samples into the table:
We will now implement SQL INTERSECT.
SELECT name FROM Employees
INTERSECT
SELECT name FROM Contractors;
The common names between the two tables, Employees and Contractors, will be located by this code. The common names Alice, Bob, and Charlie are visible in the result set, as seen in the image above.
SELECT department FROM Employees
INTERSECT
SELECT department FROM Contractors;
We notice that the result set does not include the Sales department since it is not present in the Employees table.
SELECT name, department FROM Employees
INTERSECT
SELECT name, department FROM Contractors;
We see that the result set includes records with the same name and department from both tables.
SELECT name
FROM Employees
WHERE department = 'Engineering'
INTERSECT
SELECT name
FROM Contractors
WHERE department = 'Engineering';
This makes our filter more advanced, we can exactly find records which satisfy our condition. We can see that a common name with Engineering department present in both the tables is Bob.
SELECT name
FROM Employees
WHERE salary > 50000
INTERSECT
SELECT name
FROM Contractors
WHERE hourly_rate > 50
ORDER BY name;
From the statement we can see that we are able to filter our names which have a salary above 50000 and hourly rate above 50 and also present in both the tables. After all these conditions we sort them using the ORDER BY clause. We can implement complex conditions with the help of INTERSECT and other clauses.
We have seen some of the use cases, sample examples of INTERSECT. We can see that it is a very useful tool from SQL. With a good understanding of INTERSECT we can get the best out of our data finding insights, cross verification of consistency, etc.
A. Its purpose is to identify the shared records between two or more result sets derived from select statements.
A. Both the SELECT statements involved in INTERSECT should have the same number of columns in the same order with compatible data types.
A. Yes, you can use the WHERE clause within the individual SELECT statements to filter data before the intersection is performed.
A. Only the INTERSECT operation’s final result set is eligible to employ the ORDER BY clause. You cannot use ORDER BY clauses within the individual SELECT statements.