×

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:

  1. UNION Operator.
  2. UNION ALL’ Operator.
  3. INTERSECT Operator.
  4. 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

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERIDWORKING_HOURSGENDER
1001VAIBHAVIMISHRA65000PUNEORACLE112F
1002VAIBHAVSHARMA60000NOIDAORACLE19M
1003NIKHILVANI50000JAIPURFMW210M
2001PRACHISHARMA55500CHANDIGARHORACLE110F
2002BHAVESHJAIN65500PUNEFMW212M
2003RUCHIKAJAIN50000MUMBAITESTING49F
3001PRANOTISHENDE55500PUNEJAVA39F
3002ANUJAWHERE50500JAIPURFMW29F
3003DEEPAMJAUHARI58500MUMBAIJAVA312M
4001RAJESHGOUD60500MUMBAITESTING410M

Table2: Employee.

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERIDWORKING_HOURSGENDER
1001VaibhavSharma65000PUNEORACLE112M
1002NikhilVani60000NOIDAORACLE19M
1003VaibhaviMishra50000JAIPURFMW210F
2001RuchikaJain55500CHANDIGARHORACLE110F
2002PrachiSharma65500PUNEFMW212F
2003BhaveshJain50000MUMBAITESTING49M
3001DeepamJauhari55500PUNEJAVA39M
3002ANUJAWHERE50500JAIPURFMW29F
3003PranotiShende58500MUMBAIJAVA312F
4001RAJESHGOUD60500MUMBAITESTING410M

Table3: Manager.

Manageridmanager_namemanager_department
1Snehdeep KaurORACLE
2Kirti KirtaneFMW
3Abhishek ManishJAVA
4Anupam MishraTESTING

Table4: Manager1.

Manageridmanager_namemanager_department
1Ishita AgrawalORACLE
2Kirti KirtaneFMW
3Abhishek ManishJAVA
4Paul OakipTESTING

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:

SET Operators in SQL

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:

SET Operators in SQL

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:

SET Operators in SQL

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:

SET Operators in SQL

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:

SET Operators in SQL

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:

SET Operators in SQL

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:

SET Operators in SQL

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:

SET Operators in SQL

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:

SET Operators in SQL

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:

SET Operators in SQL

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:

SET Operators in SQL

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:

SET Operators in SQL

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:

SET Operators in SQL

Only unmatched records will get displayed from both the tables, as we performed MINUS operation between Manager table and Manager1 table.


Related Topics

SQL ORDER BY

SQL ORDER BY The SQL ORDER BY clause is used to sort the data stored in tables in the database. The sorting can be done in an ascending way, descending way,...

5 minutes read.

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands. What is Drop Command? Drop is a Data Definition Language command in...

6 minutes read.

Where Condition in SQL

WHERE clause is used with the Data Manipulation Language Command in SQL, the Data Manipulation Language commands are the SELECT statement, UPDATE statement, and DELETE statement. WHERE clause condition is an...

5 minutes read.

SQL DROP Database

This tutorial will help us understand how to drop a database from the SQL Server with a few examples. The DROP command removes all the objects stored in the database and...

4 minutes read.

SQL SELECT OR Operator

This SQL tutorial explains and helps us understand how to use the OR operator in the SELECT query with examples. The OR operator in the Structured Query Language is used to...

3 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 minutes read.

How to create a database in SQL?

How to create a database in SQL It is very necessary to create a database in order to store the data into the database.Database name must always be unique.SQL does not...

6 minutes read.

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the...

5 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

SQL INSERT Table

In this tutorial, we will help you to understand and learn how to add records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

SQL Except

In SQL, we probably use the JOIN clause to receive the combined result from one or more than one table. But sometimes, we want a result that contains data from...

4 minutes read.

SQL Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value. Here, multiple rows of data are...

4 minutes read.

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples. The HAVING Clause in the SQL is used as we cannot use the WHERE...

3 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

4 minutes read.

How to use HAVING clause in SQL

In this article, we will learn about the HAVING clause concept and how to use it in SQL. What is the HAVING clause? In Structured Query Language, HAVING Clause used with GROUP...

7 minutes read.

SQL Right Join

The SQL Right Join query displays all the table records and similar records from the left table. The query display zero records if it doesn’t find any similar records. If...

4 minutes read.

Codd’s Rules in SQL

Codd’s Rules Dr. Edgar F. Codd, in 1985, laid down 13 fundamental rules after doing large-scale research on the Relational Model of databases. According to him, every database must follow these...

3 minutes read.

SQL INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help...

5 minutes read.

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.

SQL Queries

In a database, queries are used to request the result set of data from the table or action on the records. A Query can answer your simple or complicated question, perform...

5 minutes read.