×

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

SQL CONSTRAINTS

SQL Constraints specifies the rules/limitations/restrictions for data present in table. SQL Constraints are specified at the time of table creation or after table creation using ALTER command. There are two...

5 minutes read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

SQL SELECT Statement

The SQL SELECT statement is used to retrieve the data from the tables. We can also retrieve the selected records from the table using the query condition. The SQL SELECT...

5 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 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 INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.

SQL SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

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

While writing SQL queries, we can see many values which are to be converted into another datatype for accessing them. In SQL this conversion is generally done by CAST function. CAST...

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.

SQL Left Join

The SQL Left Join query displays all the records from the table and displays similar records from the right table. The query displays zero records if it doesn’t find any...

4 minutes read.

SQL Auto Increment

As we all know, In SQL for unique identification, we assign a column with the primary key. But in some tables, we find difficult to differentiate a column as a...

5 minutes read.

SQL Arithmetic Operators

This page contains all the information, learn about SQL Arithmetic Operators concept in the SQL table with the help of examples. The Arithmetic Operators is used to perform mathematical calculations on...

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

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.

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.

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 Commands

SQL commands are classified into four groups on the basis of their nature. DDL (Data Definition Language) DML (Data Manipulation Language) DCL (Data Control Language) DQL (Data Query Language) NOTE: This...

1 minute read.

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

6 minutes read.

SQL Cloning Tables

Cloning a Table: To create a copy of the table. To perform the operations, without affecting the actual table. Steps for creating a Cloning Table: Step 1: Empty Table Creation The syntax for Creating...

3 minutes read.