How to use set operators in SQL
SQL allows us to use set operators within our queries. Set operators combine the results of two or more sub-queries into a single result table. Queries containing set operators are referred to as compound queries.
Set operators in SQL include:
- UNION (∪)
- INTERSECT (∩)
- MINUS (–)
Union
We use the UNION operator to return a result table that contains all rows from the referenced tables.
UNION removes all duplicate rows from the result table.
Here’s an example:
SELECT 2 NUM FROM DUALUNIONSELECT 4 FROM DUALUNIONSELECT 3 FROM DUALUNIONSELECT 2 FROM DUALUNIONSELECT 2 FROM DUAL;
In the above example, notice how the result contains no duplicates. Each unique data row from each referenced table becomes part of the result table.
Another variation of the UNION operator is the UNION ALL operator.
The UNION ALL operator also includes duplicate rows in the result table. It is faster than the UNION operator, since it does not spend time removing duplicates.
Running the same query with UNION ALL, we get the following:
SELECT 2 NUM FROM DUALUNION ALLSELECT 4 FROM DUALUNION ALLSELECT 3 FROM DUALUNION ALLSELECT 2 FROM DUALUNION ALLSELECT 2 FROM DUAL;
Intersect
We use the INTERSECT operator to return a result table containing only matching rows from the referenced tables.
The result table contains no duplicates. Let’s look at an example:
CREATE TABLE Fruits(Fruit_name varchar(20) PRIMARY KEY);CREATE TABLE Citrus_Fruits(Fruit_name varchar(20) PRIMARY KEY);INSERT INTO FruitsVALUES ('Apple');INSERT INTO FruitsVALUES ('Mango');INSERT INTO FruitsVALUES ('Orange');INSERT INTO FruitsVALUES ('Lemon');INSERT INTO FruitsVALUES ('Pear');INSERT INTO FruitsVALUES ('Cherry');INSERT INTO Citrus_FruitsVALUES ('Orange');INSERT INTO Citrus_FruitsVALUES ('Lemon');INSERT INTO Citrus_FruitsVALUES ('Lime');INSERT INTO Citrus_FruitsVALUES ('Grape Fruit');/*USING INTERSECT OPERATOR*/SELECT f.Fruit_name FROM Fruits fINTERSECTSELECT c.Fruit_name FROM Citrus_Fruits c;
The above query returns the following table:
Fruit_name |
Lemon |
Orange |
In this example, the result table contains data rows that are common to both the Fruits and Citrus_Fruits tables.
Once again, the result table does not contain duplicates.
Note: The
INTERSECToperator is not supported by MySQL. However, we can easily simulate this type of query using either theINclause or theEXISTSclause.
Minus
We use the MINUS operator to return a table containing data rows representative of the set difference between the referenced tables.
Put simply, MINUS returns rows that are present in the first query but absent in the second query, with no duplicates.
The following example shows the behavior of the MINUS clause:
CREATE TABLE Fruits(Fruit_name varchar(20) PRIMARY KEY);CREATE TABLE Citrus_Fruits(Fruit_name varchar(20) PRIMARY KEY);INSERT INTO FruitsVALUES ('Apple');INSERT INTO FruitsVALUES ('Mango');INSERT INTO FruitsVALUES ('Orange');INSERT INTO FruitsVALUES ('Lemon');INSERT INTO FruitsVALUES ('Pear');INSERT INTO FruitsVALUES ('Cherry');INSERT INTO Citrus_FruitsVALUES ('Orange');INSERT INTO Citrus_FruitsVALUES ('Lemon');INSERT INTO Citrus_FruitsVALUES ('Lime');INSERT INTO Citrus_FruitsVALUES ('Grape Fruit');/*USING MINUS OPERATOR*/SELECT f.Fruit_name FROM Fruits fMINUSSELECT c.Fruit_name FROM Citrus_Fruits c;
The above query returns the following table:
Fruit_name |
Apple |
Cherry |
Mango |
Pear |
In this example, the result table contains all rows of the Fruits table that are not part of the Citrus_Fruits table.
Note: The
MINUSoperator is replaced by theEXCEPTclause in the SQL Server.
MySQL does not supportMINUS/EXCEPT. However, we can easily simulate this type of query using either theNOT EXISTS,NULL, orNOT INclause.
Free Resources