×

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

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 INTERSECT

SQL intersect operator is used to combine two or more SELECT statements, but it only displays the data similar to the SELECT statement. The syntax for the INTERSECT operation: SELECT COLUMN_NAME1, COLUMN_NAME2,...

3 minutes read.

SQL Alter Table

In Structured Query Language, if you want to add columns in an existing table, then modify the table, or delete columns from the table. All these operations are allowed only...

7 minutes read.

SQL WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

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 FULL JOIN

In this section, we will help you understand the concept of the SQL FULL Join clause with a few examples. The SQL FULL Join query is executed to display the integrated...

3 minutes read.

SQL KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst...

3 minutes read.

Save Point in SQL

In SQL, the classification is done into 4 languages. They are Data Definition Language (DDL)Data Manipulation Language (DML)Transaction Control Language (TCL)Data Control Language (DCL) Save Point falls under the Transaction Control Language....

4 minutes read.

SQL Formatter

As a beginner, one does not focus much on Formatting the queries while writing their code. This leads to a great confusion while referring the code in future. For a...

5 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 minutes read.

Truncate function in SQL

The TRUNCATE is a numeric function in SQL which truncates the number according to the particular decimal points. Syntax of TRUNCATE Function SELECT TRUNCATE(X, D) AS Alias_Name; In the TRUNCATE syntax, X...

4 minutes read.

SQL INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

SQL JOIN

As the name says, joins mean to merge something or to combine something. But in the case of SQL, joins mean to merge or combine two different tables. The Join clause...

12 minutes read.

SQL Syntax

In this tutorial, we will help you understand about the different SQL Syntax concepts with the help of an example. Every Structured Query Language starts with Keywords like select, insert, update,...

5 minutes read.

SQL FOREIGN KEY

In this article, we will learn about the FOREIGN KEY constraints and how to define a FOREIGN KEY constraint to build the relationship between two tables. In a Relational Databases Management...

4 minutes read.

SQL SubQuery

The Sub-query in the SQL is the inner query placed or positioned inside another query, which is also known is the outer query. The inner query is embedded in the...

6 minutes read.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

6 minutes read.

SQL UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

3 minutes read.

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.

SQL SELECT LIKE Operator

The SQL SELECT LIKE Operator tutorial helps us understand how to use the LIKE operator in the SELECT query with examples. The SQL SELECT LIKE Operator retrieves the records from the...

4 minutes read.