Learn Set Operators In SQL (UNION, UNION ALL, INTERSECT, EXCEPT)
Summary: In this tutorial, you will learn about Set Operators in SQL such as UNION, UNION ALL, INTERSECT, EXCEPT With Practical Example. Let’s understand the set operators in SQL
Introduction to SQL Set Operators
Set Operators in SQL are basically used to combine the same type of data from two or more tables. In another word you can say that SQL Set operators are used to join the results of two (or more) SELECT statements. Therefore, although more than one select statement will then be present, only one result set is coming back or returned.
Venn Diagram Presentation:-
The UNION set operator in SQL returns the combined results of the two SELECT statements. primarily, it removes the duplicates from the results means that only one row will be listed for every duplicated result. To counter this behavior, you can use the UNION ALL set operator that retains the duplicates in the final result. Furthermore, In SQL INTERSECT lists only records that are common to both the SELECT queries. when in the SQL Server the MINUS set operator removes the 2nd query’s results from the output if they are also found in the 1st query’s results. Thus, INTERSECT and MINUS set operations in SQL to produce unduplicated results.
Types of Set Operators In SQL
There are four basically types of SQL operators, such as:
- Union
- Union all
- Intersect
- Minus
Here is an abstract table for whatever we’ll be learning during this set operators SQL article.
SQL Set Operator | Function/Returns |
---|---|
Union | Combines distinct results of two or more SELECT statements, without duplicates. |
Union All | Combines all results of two or more SELECT statements, including all duplicates. |
Intersect | Returns only the common records obtained the data from two or more SELECT statements. |
Minus | Returns only those records which are exclusive to the first table (i.e. no matching to each other) |
Syntax of Set Operators
1 2 3 4 5 |
SELECT [Column_Name, . . . ] FROM [Table1] [SET OPERATOR] SELECT [Column_Namse, . . .] FROM [Table2] [SET OPERATOR] ... ... SELECT [Column_Name, . . . ] FROM [TableN] |
Parameters: The different parameters utilized in the syntax are:-
- SET OPERATOR: Here, you can mention the type of set operation such as Union, Union all, Intersect, Minus which you want to perform.
- Column_Name: Mention the column name on which you wish to perform the set operation & want in the result set.
- FROM Table2: Mention the first table name from which the column has to be fetched
- FROM Table2: Mention the second table name from which the column has to be fetched
Above all mentioned parameters, are mandatory. You can use WHERE GROUP BY and HAVING clauses as per your requirements.
For Demonstration Purpose: A schema for the discussed tables is given below as follows:
Table1: Dell_Customers
CustomersID | Name | City |
---|---|---|
11 | Aditya Shukla | Lucknow |
12 | Sumit Yadav | Kanpur |
13 | Anita Singh | Jaipur |
14 | Satendra Kumar | Lucknow |
15 | Pushpendra Verma | Noida |
16 | Saumya Tripathi | Gr. Noida |
17 | Shivaditya Shukla | Kolkata |
18 | Jaishankar Tripathi | Ghaziabad |
19 | Manmohan Kumar | Lucknow |
20 | Tushar Dixit | Surat |
Table2: HP_Customers
CustomersID | Name | City |
---|---|---|
11 | Abhishek Thakur | Jaipur |
12 | Tushar Dixit | Kanpur |
13 | Shivaditya Shukla | Mumbai |
14 | Pankaj Garg | Noida |
15 | Saumya Tripathi | Gr. Noida |
16 | Ram Kumar | Jhasi |
17 | Sunita Yadav | Kanpur |
18 | Mohit Chauhan | Kolkata |
19 | Pushpendra Verma | Noida |
20 | Kabir Singh | Nainital |
Practical Examples of Set Operators In SQL
1) Union Set Operator
The UNION set operator in SQL is used when you need to combine the results obtained from two or more SELECT statements. The given below example illustrates the use of the SQL UNION Operator.
Example: Let us find the name of all the customers
1 2 3 |
SELECT Name FROM Dell_Customers UNION SELECT Name FROM HP_Customers; |
You will notice that there are two customers named Shivaditya Shukla, one is from Mumbai and another one from Kolkata. But the SQL Union operator returned only one customer name. so as to resolve the above-mentioned problem, we can use the SQL UNION ALL operator.
2) Union All Set Operator
The SQL UNION set operator is used to combining all the results obtained from two or more SELECT statements. Therefore, unlike the Union operator, it considers duplicate values & includes them in the final result.
Here is an example to illustrate the How to use of UNION ALL Operator.
Example:Find the names of all the customers from table Dell_Customers and HP_Customers.
1 2 3 |
SELECT Name FROM Dell_Customers UNION ALL SELECT Name FROM HP_Customers; |
3) Intersect Set Operator
The SQL intersect set operator is used to combine all the results of two SELECT statements. But it returns only those records that are common to both the SELECT statements.
The given below is an example to illustrate the use of the INTERSECT Operator in SQL.
Example: Find the details of customers who Purchase HP and DELL laptops.
1 2 3 |
SELECT Name, City FROM Dell_Customers INTERSECT SELECT Name, City FROM HP_Customers; |
4) Minus Set Operator
The MINUS set operator in SQL is used to combine all the results of two or more SELECT statements. Furthermore, but they return only those records that are present exclusively in the first table.
Therefore, The given below is an example to illustrate the use of the EXCEPT Operator.
Example: Find the details of customers who shopped only DELL laptops but not HP laptops.
1 2 3 |
SELECT Name FROM Dell_Customers EXCEPT SELECT Name FROM HP_Customers; |