You are on page 1of 5

The INTERSECT operator is a kind of SET operation in SQL that includes UNION,

UNION ALL, MINUS, and INTERSECT. The INTERSECT operator returns the distinct
(common) elements in two sets or common records from two or more tables. In
other words, it compares the result obtained by two queries and produces unique
rows, which are the result returned by both queries.

Syntax
The following are the syntax that illustrates the use of the INTERSECT operator:

1. SELECT column_lists FROM table_name WHERE condition


2. INTERSECT
3. SELECT column_lists FROM table_name WHERE condition;

Simulation of MySQL INTERSECT Operator


Since MySQL does not provide support for the INTERSECT operator. However, we can
use the INNER JOIN and IN clause to emulate this operator.

Let us first creates two tables with the following structure to understand the
INTERSECT operator:

Table1 structure and data:

1. mysql> CREATE TABLE tab1 (


2. Id INT PRIMARY KEY
3. );
4.
5. mysql> INSERT INTO tab1 VALUES (1), (2), (3), (4);

Table2 structure and data:

1. mysql> CREATE TABLE tab2 (


2. id INT PRIMARY KEY
3. );
4.
5. mysql> INSERT INTO tab2 VALUES (3), (4), (5), (6);
MySQL Union
MySQL Union is an operator that allows us to combine two or more results from
multiple SELECT queries into a single result set. It comes with a default feature that
removes the duplicate rows from the result set. MySQL always uses the name of the
column in the first SELECT statement will be the column names of the result
set(output).

MySQL Union must follow these basic rules:

o The number and order of the columns should be the same in all tables that you are
going to use.
o The data type must be compatible with the corresponding positions of each select
query.
o The column name selected in the different SELECT queries must be in the same order.

MySQL Union Syntax


The following are the syntax of Union operator in MySQL:

1. SELECT column_list FROM table1


2. UNION
3. SELECT column_list FROM table2;

We can understand the Union operator with the following visual representation:

In the above image, we can see that the Union operator removes the duplicate rows
and returns unique rows only.
MySQL MINUS
The MINUS operator is a kind of SET operation in SQL which also includes
INTERSECT, UNION, and UNION ALL. The MINUS operator returns the unique
element from the first table/set, which is not found in the second table/set. In
other words, it will compare the results of two queries and produces the resultant
row from the result set obtained by the first query and not found in the result set
obtained by the second query.

Syntax
The following are the syntax that illustrates the use of a MINUS operator:

1. SELECT column_lists FROM table_name WHERE (condition)


2. MINUS
3. SELECT column_lists FROM table_name WHERE (condition);
NOTE: MySQL does not provide support for the MINUS operator. This article
shows you how to simulate the MINUS query in MySQL using the JOIN clause.

The following are the rules for a statement that uses the MINUS operator:

o The number and order of columns in all the SELECT statements must be the same.
o The data types of the corresponding columns in both SELECT statements must be the
same or convertible.

We can understand the MINUS operator with the following visual representation.
Here this operator returns the distinct/unique values from the result obtained by
the first SELECT statement and not found in the result obtained by the second
SELECT statement:
Simulation of MySQL MINUS Operator
Since MySQL does not provide support for MINUS operator. However, we can use
a LEFT JOIN clause to simulate this operator.

We can use the following syntax to simulate the MINUS operator:

1. SELECT column_list FROM table1


2. LEFT JOIN table2 ON condition
3. WHERE table2.column_name IS NULL;

Example

Let us first creates two tables with the following structure for the demonstration:

Table1 structure and data:

1. mysql> CREATE TABLE tab1 (


2. Id INT PRIMARY KEY
3. );
4.
5. mysql> INSERT INTO tab1 VALUES (1), (2), (3), (4);

Table2 structure and data:

1. mysql> CREATE TABLE tab2 (


2. id INT PRIMARY KEY
3. );
4.
5. mysql> INSERT INTO tab2 VALUES (3), (4), (5), (6);
Now, we will execute the below query with the help of the LEFT JOIN clause. It gives
the same result as the MINUS operator:

1. mysql> SELECT Id FROM tab1


2. LEFT JOIN tab2 USING (Id)
3. WHERE tab2.Id IS NULL;

Output:

After executing the above query, we should get the output as below image:

You might also like