SET Operators in SQL
The operator used to join or combine two queries is none other than SET operators. Operators categorized into SET operators are as follows:
- UNION Operator.
- UNION ALL’ Operator.
- INTERSECT Operator.
- MINUS Operator.
Rules to be followed to operate using SET Operator are as follows:
- The number of Columns and Order of Columns must be the same.
- Data Type must be Compatible.
Let’s understand each of the SET Operators with examples.
Consider the following tables along with the given records.
Table1: Employees
EMPLOYEEID | FIRST_NAME | LAST_NAME | SALARY | CITY | DEPARTMENT | MANAGERID | WORKING_HOURS | GENDER |
1001 | VAIBHAVI | MISHRA | 65000 | PUNE | ORACLE | 1 | 12 | F |
1002 | VAIBHAV | SHARMA | 60000 | NOIDA | ORACLE | 1 | 9 | M |
1003 | NIKHIL | VANI | 50000 | JAIPUR | FMW | 2 | 10 | M |
2001 | PRACHI | SHARMA | 55500 | CHANDIGARH | ORACLE | 1 | 10 | F |
2002 | BHAVESH | JAIN | 65500 | PUNE | FMW | 2 | 12 | M |
2003 | RUCHIKA | JAIN | 50000 | MUMBAI | TESTING | 4 | 9 | F |
3001 | PRANOTI | SHENDE | 55500 | PUNE | JAVA | 3 | 9 | F |
3002 | ANUJA | WHERE | 50500 | JAIPUR | FMW | 2 | 9 | F |
3003 | DEEPAM | JAUHARI | 58500 | MUMBAI | JAVA | 3 | 12 | M |
4001 | RAJESH | GOUD | 60500 | MUMBAI | TESTING | 4 | 10 | M |
Table2: Employee.
EMPLOYEEID | FIRST_NAME | LAST_NAME | SALARY | CITY | DEPARTMENT | MANAGERID | WORKING_HOURS | GENDER |
1001 | Vaibhav | Sharma | 65000 | PUNE | ORACLE | 1 | 12 | M |
1002 | Nikhil | Vani | 60000 | NOIDA | ORACLE | 1 | 9 | M |
1003 | Vaibhavi | Mishra | 50000 | JAIPUR | FMW | 2 | 10 | F |
2001 | Ruchika | Jain | 55500 | CHANDIGARH | ORACLE | 1 | 10 | F |
2002 | Prachi | Sharma | 65500 | PUNE | FMW | 2 | 12 | F |
2003 | Bhavesh | Jain | 50000 | MUMBAI | TESTING | 4 | 9 | M |
3001 | Deepam | Jauhari | 55500 | PUNE | JAVA | 3 | 9 | M |
3002 | ANUJA | WHERE | 50500 | JAIPUR | FMW | 2 | 9 | F |
3003 | Pranoti | Shende | 58500 | MUMBAI | JAVA | 3 | 12 | F |
4001 | RAJESH | GOUD | 60500 | MUMBAI | TESTING | 4 | 10 | M |
Table3: Manager.
Managerid | manager_name | manager_department |
1 | Snehdeep Kaur | ORACLE |
2 | Kirti Kirtane | FMW |
3 | Abhishek Manish | JAVA |
4 | Anupam Mishra | TESTING |
Table4: Manager1.
Managerid | manager_name | manager_department |
1 | Ishita Agrawal | ORACLE |
2 | Kirti Kirtane | FMW |
3 | Abhishek Manish | JAVA |
4 | Paul Oakip | TESTING |
UNION Operator
UNION Operator is the First Operator used to merge or combine the two or more SELECT statements. If and only if their number of columns and order of columns are the same.
The duplicated rows will not be considered in the result obtained after the UNION operation is performed.
The syntax for UNION operation is as follows,
SELECT * FROM TABLE_NAME1 UNION SELECT * FROM TABLE_NAME2;
Example 1: Execute a query to perform UNION operation between Employees table and Employee table.
SELECT * FROM EMPLOYEES UNION SELECT * FROM EMPLOYEE;
In the above query, we have used two SELECT queries. The First SELECT query retrieves the data from employees and the Second SELECT query retrieves the data from employee data, and UNION operation is performed on both the SELECT queries.
UNION operation will discard duplicates rows between both the tables. The following output is shown as:
Both table records are displayed except duplicate records.
Example 2: Execute a query to perform UNION operation between Employees table and Employee table. But display only those employees’ records from employees table that belong to city Mumbai and from employee table only those employees’ records whose employee salary is greater than 50000 and less than 60000.
SELECT * FROM EMPLOYEES WHERE CITY = 'MUMBAI' UNION SELECT * FROM EMPLOYEE WHERE SALARY > 50000 AND SALARY < 60000;
In the above query, we have used two SELECT queries. The First SELECT query fetches only those records whose employee belongs to Mumbai city from the Employees table. And perform UNION operation with the second SELECT query fetches only those records whose employee salary is greater than 50000 but lesser than 60000 from the employee table. UNION operation will discard duplicates rows between both the tables.
The following output is shown as:
Both table records are displayed except duplicate records.
Example 3: Execute a query to perform UNION operation between Employees table and Employee table. We want only those records from the Employees table whose employee city belongs to 'Pune’ and ‘Jaipur’.
SELECT* FROM EMPLOYEES WHERE CITY IN ('PUNE', 'JAIPUR') UNION SELECT * FROM EMPLOYEE;
In the above query, we have used two SELECT queries. The first SELECT query fetches those records whose employees belong to 'Pune’ and ‘Jaipur' city from the Employees table. The UNION operation is performed with the records retrieved from the second SELECT query from the table employee.
UNION operation will discard duplicates rows between both the tables. The following output is shown as:
As we all can see, the result first five records are those employees who belong to Pune or Jaipur city. And rest records are from the employee table, the second SELECT query.
Both table records are displayed except duplicate records.
Example 4: Execute a query to perform UNION operation between Employees table and Employee table. We want only those records from the Employee table whose employee the first name starts with V.
SELECT * FROM EMPLOYEES UNION SELECT * FROM EMPLOYEE WHERE FIRST_NAME LIKE 'V%';
In the above query, we have used two SELECT queries. The first SELECT query fetches all the records from the Employees table. The UNION operation is performed with the records retrieved from the second SELECT query from the table employee whose first name starts with 'V'.
UNION operation will discard duplicates rows between both the tables. The following output is shown as:
As we all can first 10 results are from the Employees table, and the rest are from the Employee table whose employee the first name starts with 'V'.
Example 5: Execute a query to perform UNION operation between Manager table and Manager1 table.
SELECT * FROM MANAGER UNION SELECT * FROM MANAGER1;
In the above query, we have used two SELECT queries. The First SELECT query retrieves the data from the Manager and the Second SELECT query retrieves the data from manager1 data, and UNION operation is performed on both the SELECT queries.
UNION operation will discard duplicates rows between both the tables. The following output is shown as:
Both table records are displayed except duplicate records.
UNION ALL Operators
UNION ALL operator combines all the data from both the queries. In the UNION operator, duplicates records were not considered in the result, but UNION ALL considered duplicate records in the result obtained after the UNION ALL operation is performed.
The syntax for UNION ALL operation are as follows,
SELECT * FROM TABLE_NAME1 UNION ALL SELECT * FROM TABLE_NAME2;
Example 1: Execute a query to perform UNION ALL operation between Employees table and Employee table.
SELECT * FROM EMPLOYEES UNION ALL SELECT * FROM EMPLOYEE;
In the above query, we have used two SELECT queries. The First SELECT query fetches the data from employees and performs UNION ALL operation with the data fetched by the Second SELECT query retrieves the data from the employee table.
The following output is shown as:
All the records will get displayed from both the tables Employees and Employee, duplicates records also get displayed as we perform UNION ALL operation.
Example 2: Execute a query to perform UNION ALL operation between Employees table and Employee table. But display only those employees' records from employees table that belong to city Mumbai. From the employee table, only those employees' records whose employee salary is 60500 and city is ‘Mumbai’.
SELECT * FROM EMPLOYEES WHERE CITY = 'MUMBAI' UNION ALL SELECT * FROM EMPLOYEE WHERE SALARY = 60500 AND CITY = 'MUMBAI';
In the above query, we have used two SELECT queries. The First SELECT query fetches only those records whose employee belongs to Mumbai city from the Employees table. And perform UNION ALL operation with the second SELECT query fetches only those records whose employee salary is 60500 and city is ‘Mumbai’ from the employee table.
The following output is shown as:
Both table records are displayed, including duplicate records as we perform UNION ALL operations.
Example 3: Execute a query to perform UNION operation between Employees table and Employee table. We want only those records from the Employees table whose employee city belongs to 'Pune’, ‘Mumbai’ and ‘Jaipur’.
SELECT* FROM EMPLOYEES WHERE CITY IN ('PUNE', 'MUMBAI', 'JAIPUR') UNION ALL SELECT * FROM EMPLOYEE;
In the above query, we have used two SELECT queries. The first SELECT query fetches those records whose employees belong to 'Pune’, ‘Mumbai’ and ‘Jaipur' city from the Employees table. The UNION ALL operation is performed with the records retrieved from the second SELECT query from the table employee.
The following output is shown as:
Both table records are displayed, including duplicate records as we perform UNION ALL operations.
Example 4: Execute a query to perform UNION ALL operation between Manager table and Manager1 table.
SELECT * FROM MANAGER UNION ALL SELECT * FROM MANAGER1;
In the above query, we have used two SELECT queries. The First SELECT query fetches the data from Manager and performs UNION ALL operations with the data fetched by the Second SELECT query retrieves the data from the manager1 table.
The following output is shown as:
All the records will be displayed from both the tables Manager and Manager1; duplicate records are displayed as we perform UNION ALL operations.
INTERSECT Operators
Intersect operator is used to combining two or more SELECT statements, but it only displays the data similar to the SELECT statement.
The syntax for INTERSECT operation is as follows,
SELECT * FROM TABLE_NAME1 INTERSECT SELECT * FROM TABLE_NAME2;
Example 1: Execute a query to perform INTERSECT operation between Employees table and Employee table.
SELECT * FROM EMPLOYEES INTERSECT SELECT * FROM EMPLOYEE;
In the above query, we have used two SELECT queries. The First SELECT query fetches the data from employees and performs INTERSECT operation with the data fetched by the Second SELECT query retrieves the data from the employee table.
The following output is shown as:
Only similar records will get displayed from both the tables, as we performed INTERSECT operation between Employees table and Employee table.
Example 2: Execute a query to perform INTERSECT operation between Manager table and Manager1 table.
SELECT * FROM MANAGER INTERSECT SELECT * FROM MANAGER1;
In the above query, we have used two SELECT queries. The First SELECT query fetches the data from Manager and performs INTERSECT operation with the data fetched by the Second SELECT query retrieves the data from Manager1 table.
The following output is shown as:
Only similar records will get displayed from both the tables, as we performed INTERSECT operation between Manager table and Manager1 table.
MINUS Operators
MINUS operator is used to returning the rows present in the first query but absent in the rest of the queries with no duplicates.
The syntax for MINUS operation is as follows.
SELECT * FROM TABLE_NAME1 EXCEPT SELECT * FROM TABLE_NAME2;
Note: MINUS keyword is supported only in ORACLE databases. We can use EXCEPT keyword for other databases to perform a similar operation.
Example 1: Execute a query to perform MINUS operation between Employees table and Employee table.
SELECT * FROM EMPLOYEES EXCEPT SELECT * FROM EMPLOYEE;
In the above query, we have used two SELECT queries. The First SELECT query fetches the data from employees and performs MINUS operation with the data fetched by the Second SELECT query retrieves the data from the employee table.
The following output is shown as:
Only unmatched records will get displayed from both the tables, as we performed MINUS operation between Employees table and Employee table.
Example 2: Execute a query to perform MINUS operation between Manager table and Manager1 table.
SELECT * FROM MANAGER EXCEPT SELECT * FROM MANAGER1;
In the above query, we have used two SELECT queries. The First SELECT query fetches the data from Manager and performs MINUS operation with the data fetched by the Second SELECT query retrieves the data from Manager1 table.
The following output is shown as:
Only unmatched records will get displayed from both the tables, as we performed MINUS operation between Manager table and Manager1 table.