×

SQL SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples.

The SQL SET Operator is used to merge the output of two or more than two SELECT queries.

The operator falls under the SET operator category is as follows:

  1. SQL UNION Operator
  2.  SQL UNION ALL Operator
  3.  SQL INTERSECT Operator
  4.  SQL MINUS Operator

There are some rules and regulations to be followed to execute queries using the SET Operator in SQL. Rules are as follows:

  1.  There should be equal numbers of columns, and the order of columns must be the same between both the tables.
  2.  Data Types must be compatible.

Let's learn the SQL SET Operator in detail with the help of an example.

Consider the already existing table with the following data

Table Number 1: Student

Student_IdStudent_NameCityAge
1Pratik SrivastavPune23
2Utkarsh RokadeMumbai22
3Sourabh ChougaleNashik23
4Prateek ZimbrePune24
5Sakshi PatilAurangabad22
6Shruti SharmaMumbai21
7Pranoti ShendeAurangabad23
8Harshada DhanwatNashik24
9Tejas BairagiNashik21
10Nikhil PatilPune24
11Samaira SharmaMumbai22
12Anushka SenAurangabad23
13Bhushan PachpandeBangalore25
14John ChaudharyBangalore24
15Sonakshi SenBangalore25
16Mayuri WaghHyderabad22
17Ritika PatilHyderabad21
18Tushar MahaleHyderabad23

Table Number 2: Students

Student_IdStudent_NameCityAge
1Pratik SrivastavPune23
2Utkarsh RokadeMumbai22
3Sourabh ChougaleNashik23
4Prateek ZimbrePune24
5Swati SharmaPune22
6Nikita IngaleMumbai21
7Pranoti ShendeAurangabad23
8Harshada KakadeNashik24
9Tejas BairagiNashik21
10Naman SharmaAurangabad24
11Samaira SharmaMumbai22
12Anushka SenAurangabad23
13Bhavesh JainBangalore25
14Jayesh NikamBangalore24
15Sonalika ShindeBangalore25
16Mayuri WaghHyderabad22
17Ritika PatilHyderabad21
18Tushar MahaleHyderabad23

1. SQL UNION Operator

  • SQL UNION Operator is used to join or combine the two or more than two SELECT queries.
  • The common records are not considered in the output result obtained after the UNION operator is executed.

The syntax for SQL UNION Operator is as follows:

SELECT Column_Name_1, Column_Name_2, Column_Name_3, Column_Name_4 FROM Table_Name_1 UNION SELECT Column_Name_1, Column_Name_2, Column_Name_3, Column_Name_4 FROM Table_Name_2;

Example: Write a query to execute UNION operation between student table and students table.

SELECT * FROM Student UNION SELECT * FROM Students;

In the above query, we have executed two SELECT statements. The first SELECT statement displays the data from the Student table and executes a UNION operation with the data retrieved by the second SELECT statement from the Students table.

The output of the above query is as follows:

Student_IdStudent_NameCityAge
1Pratik SrivastavPune23
2Utkarsh RokadeMumbai22
3Sourabh ChougaleNashik23
4Prateek ZimbrePune24
5Sakshi PatilAurangabad22
6Shruti SharmaMumbai21
7Pranoti ShendeAurangabad23
8Harshada DhanwatNashik24
9Tejas BairagiNashik21
10Nikhil PatilPune24
11Samaira SharmaMumbai22
12Anushka SenAurangabad23
13Bhushan PachpandeBangalore25
14John ChaudharyBangalore24
15Sonakshi SenBangalore25
16Mayuri WaghHyderabad22
17Ritika PatilHyderabad21
18Tushar MahaleHyderabad23
5Swati SharmaPune22
6Nikita IngaleMumbai21
8Harshada KakadeNashik24
10Naman SharmaAurangabad24
13Bhavesh JainBangalore25
14Jayesh NikamBangalore24
15Sonalika ShindeBangalore25
SQL SET Operator

2. SQL UNION ALL Operatorss

  • SQL UNION ALL Operator merges all the records from the SELECT statement used in the query.
  • Common data are not displayed in the output in the SQL UNION operator. Still, SQL UNION ALL operator allows common data to be displayed in the output obtained after the UNION ALL operation is executed.

The syntax for SQL UNION ALL Operator is as follows:

SELECT Column_Name_1, Column_Name_2, Column_Name_3, Column_Name_4 FROM Table_Name_1 UNION ALL SELECT Column_Name_1, Column_Name_2, Column_Name_3, Column_Name_4 FROM Table_Name_2;

Example 1: Write a query to execute UNION ALL operation between student table and student’s table.

SELECT * FROM Student UNION ALL SELECT * FROM Students;

In the above query, we have executed two SELECT statements. The first SELECT statement displays the data from the Student table and executes a UNION ALL operation with the data retrieved by the second SELECT statement from the Students table.

The output of the above query is as follows:

Student_IdStudent_NameCityAge
1Pratik SrivastavPune23
2Utkarsh RokadeMumbai22
3Sourabh ChougaleNashik23
4Prateek ZimbrePune24
5Sakshi PatilAurangabad22
6Shruti SharmaMumbai21
7Pranoti ShendeAurangabad23
8Harshada DhanwatNashik24
9Tejas BairagiNashik21
10Nikhil PatilPune24
11Samaira SharmaMumbai22
12Anushka SenAurangabad23
13Bhushan PachpandeBangalore25
14John ChaudharyBangalore24
15Sonakshi SenBangalore25
16Mayuri WaghHyderabad22
17Ritika PatilHyderabad21
18Tushar MahaleHyderabad23
1Pratik SrivastavPune23
2Utkarsh RokadeMumbai22
3Sourabh ChougaleNashik23
4Prateek ZimbrePune24
5Swati SharmaPune22
6Nikita IngaleMumbai21
7Pranoti ShendeAurangabad23
8Harshada KakadeNashik24
9Tejas BairagiNashik21
10Naman SharmaAurangabad24
11Samaira SharmaMumbai22
12Anushka SenAurangabad23
13Bhavesh JainBangalore25
14Jayesh NikamBangalore24
15Sonalika ShindeBangalore25
16Mayuri WaghHyderabad22
17Ritika PatilHyderabad21
18Tushar MahaleHyderabad23
SQL SET Operator

Example 2: Write a query to execute UNION ALL operation between student table and students tables. Display only those records from the Student table where the student resides in the 'Bangalore', 'Hyderabad', and 'Nashik’ cities.

SELECT * FROM Student WHERE City IN ('Nashik', 'Bangalore', 'Hyderabad') UNION ALL SELECT * FROM Students;

In the above query, we have executed two SELECT statements. The first SELECT statement displays the data from the Student table where the student resides in the 'Nashik’, 'Bangalore', and 'Hyderabad' cities. It executes a UNION ALL operation with the data retrieved by the second SELECT statement from the Students table.

The output of the above query is as follows:

Student_IdStudent_NameCityAge
3Sourabh ChougaleNashik23
8Harshada DhanwatNashik24
9Tejas BairagiNashik21
13Bhushan PachpandeBangalore25
14John ChaudharyBangalore24
15Sonakshi  SenBangalore25
16Mayuri WaghHyderabad22
17Ritika PatilHyderabad21
18Tushar MahaleHyderabad23
1Pratik SrivastavPune23
2Utkarsh RokadeMumbai22
3Sourabh ChougaleNashik23
4Prateek ZimbrePune24
5Swati SharmaPune22
6Nikita IngaleMumbai21
7Pranoti ShendeAurangabad23
8Harshada KakadeNashik24
9Tejas BairagiNashik21
10Naman SharmaAurangabad24
11Samaira SharmaMumbai22
12Anushka SenAurangabad23
13Bhavesh JainBangalore25
14Jayesh NikamBangalore24
15Sonalika ShindeBangalore25
16Mayuri WaghHyderabad22
17Ritika PatilHyderabad21
18Tushar MahaleHyderabad23
SQL SET Operator

3. SQL INTERSECTS Operators

SQL Intersect operator is used to merge two or more than two SELECT queries, but INTERSECT Operator displays only those common data between both the tables.

The syntax for SQL INTERSECT Operator is as follows:

SELECT Column_Name_1, Column_Name_2, Column_Name_3, Column_Name_4 FROM Table_Name_1 INTERSECT SELECT Column_Name_1, Column_Name_2, Column_Name_3, Column_Name_4 FROM Table_Name_2;

Example: Write a query to execute INTERSECTS operation between student table and students table.

SELECT * FROM Student INTERSECT SELECT * FROM Students;

In the above query, we have executed two SELECT statements. The first SELECT statement displays the data from the Student table and executes a INTERSECTS operation with the data retrieved by the second SELECT statement from the Students table.

The output of the above query is as follows:

Student_IdStudent_NameCityAge
1Pratik SrivastavPune23
2Utkarsh RokadeMumbai22
3Sourabh ChougaleNashik23
4Prateek ZimbrePune24
7Pranoti ShendeAurangabad23
9Tejas BairagiNashik21
11Samaira SharmaMumbai22
12Anushka SenAurangabad23
16Mayuri WaghHyderabad22
17Ritika PatilHyderabad21
18Tushar MahaleHyderabad23
SQL SET Operator

4. SQL MINUS Operators:

  • SQL MINUS operator displays the rows available in the first SELECT query that are not available in the second SELECT query.
  • MINUS keyword doesn't work in SQL query. Instead of the MINUS keyword, use EXCEPT keyword to execute the MINUS operator in the SQL.

The syntax for SQL MINUS Operator is as follows:

SELECT Column_Name_1, Column_Name_2, Column_Name_3, Column_Name_4 FROM Table_Name_1 EXCEPT SELECT Column_Name_1, Column_Name_2, Column_Name_3, Column_Name_4 FROM Table_Name_2;

Example: Write a query to execute MINUS operation between student table and students table.

SELECT * FROM Student EXCEPT SELECT * FROM Students;

In the above query, we have executed two SELECT statements. The first SELECT statement displays the data from the Student table and executes a MINUS operation with the data retrieved by the second SELECT statement from the Students table.

The output of the above query is given below:

Student_IdStudent_NameCityAge
5Sakshi PatilAurangabad22
6Shruti SharmaMumbai21
8Harshada DhanwatNashik24
10Nikhil PatilPune24
13Bhushan PachpandeBangalore25
14John ChaudharyBangalore24
15Sonakshi  SenBangalore25
SQL SET Operator

Related Topics

How to drop a column in SQL?

How to drop a column in SQL Introduction To delete a column from an already created table, one needs to use the ALTER command along with the DROP COLUMN clause. Syntax: ALTER TABLE tablename...

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

Commit and Rollback in SQL

In Structured Query Language, we have Data Definition Language (DDL) and Data Manipulation Language (DML) commands the same way we have Transaction Control Language (TCL) commands in the Structured Query...

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

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 Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

4 minutes read.

SQL SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

5 minutes read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

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

Check Constraint in SQL

The Check Constraint in SQL is the rule or set of rules used to limit the data range that can be entered in a table column. Check constraint is used...

8 minutes read.

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database. The SQL view is similar to the SQL tables. In SQL...

7 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 LOGICAL Operator

In this tutorial, we will understand the operator who falls under the logical operator in SQL with the help of examples. The SQL Logical Operator displays the query result in one...

5 minutes read.

WHERE Clause vs HAVING Clause

The WHERE Clause and HAVING clause filter the records in the Structured Query Language queries. The main difference between the WHERE clause and the HAVING clause is the WHERE clause...

5 minutes read.

SQL Insert multiple rows

This article will deal with a new insertion method in SQL which inserts multiple rows at a time. Multiple records can be inserted at a time into a specific table with...

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

Introduction to SQL

SQL Introduction SQL is Standard Query Language. This language is used to communicate or interact with database. In other words, SQL is used to access and manage data or information...

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