×

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 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 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.

Trigger in SQL

In this article, we will learn about the concept of trigger in SQL and its implementation with the help of an example. A Trigger in Structured Query Language is a set...

4 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 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.

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.

How to Create Temporary Table in SQL?

How to Create Temporary Table in SQL  Introduction to Temporary Tables Temporary table is a table which is used to store temporary data that can be used further in the same client...

3 minutes read.

What is SQL Injection?

Introduction to SQL Injection SQL injection is a vulnerability or a technique that might destroy the database of a website or a web application. It is one of the most widely...

4 minutes read.

SQL Injection

SQL injection is a technique, this may destroy the database. It is one type of hacking technique. SQL IN WEB PAGES: Injection occurs when we ask for input like an id or...

3 minutes read.

SQL SELECT MAX

The SQL Max() function is an aggregate function in SQL. This function returns the values which are greater in the condition. The condition may be a number, or it may...

5 minutes read.

SQL Tutorial for Beginners

SQL tutorial provides basic and advanced concepts of Structured Query Language and how you deploy SQL to work with a relational database system. Our SQL tutorial is designed for beginners...

3 minutes read.

SQL CREATE TABLE

In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE...

5 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.

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.

SQL SELECT AVG

In this tutorial, we will learn about the aggregate function name avg() function concept in SQL with the help of examples. The AVG() function is one of the aggregate functions in...

3 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.

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 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 GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 minutes read.

SQL DROP Table

In this tutorial, we will help you to understand how to delete the table from the database in SQL with the help of examples. The DROP TABLE query is used to...

4 minutes read.