Categories: SQL Tutorials

SQL Union All Operator Examples | How to use Union All in SQL?

In my previous article I have given the details of Union statement with real examples . In this article I would like to give the information about SQL Union all operator with real life industry examples. I would like to give the bullet-points of SQL Union all operator and examples as well. There are so many questions in interview about union as well as union all operator and other set operators. We will throw light on those interview questions of union all as well.

What you will find in this article?

  1. What are multiple types of Set operators in SQL?
  2. SQL Union all Operator with real industry examples.

What are multiple types of Set operators in SQL?

The set operators are used to perform multiple set operations on tables in database. There are following 4 basic set operators used in SQL :

  1. Union
  2. Union All
  3. Intersect
  4. Minus

The above set operators are used to perform multiple set operations on tables. You may get so many questions in interview about these set operators.

SQL Union all Operator with real industry examples

In this section we will focus on SQL union all operator with real industry examples in detail. We will have look in to multiple syntax of SQL Union all Operator with real life industry examples in detail.

  1. SQL Union all Operator combines the result of two or more tables where the column names and datatypes of the multiple tables needs to be similar.
  2. The Union all operator is used to fetch the common results which eliminates the duplicates from table.
  3. Just make sure that Every select statement within union must have same number of columns.
  4. The columns must have similar datatype and we require to follow order as well.
Union all

5.In above diagram if we see there are two sets : One set contains a,b,c and other set contains a,d,e as values. If we require to combine the dataset and used union all. It will not eliminate duplicate records and output will be a,b,c,a,d,e.

6.Union all operator does not eliminates duplicates and shows duplicate row as many times in the result.

7.The Union all operator is faster than union operator as it does not eliminate duplicates from the dataset.

8. Syntax of Union all Operator :

Syntax Of Union all :

Select column1…column n from table1;

union all

Select column1…column n from table2;

9.Real Life Industry Example :

If There are two tables Student and Student_1 and you require to find out the combined records from two tables and add it in third table. In this case we need to use the Union all Operator.

Student Table :

Roll_noStudent_nameClassMarks
1Rahul10th68
2Rohan10th55
3Rohit9th43
Student Table

Second table

Table Name:Student_1

Roll_noStudent_nameClassMarks
1Rahul10th68
4Mohit10th47
3Rohit9th43
Student_1

Query:

Select Roll_no,Student_name,Class,Marks from Student;

Union all

Select Roll_no,Student_name,Class,Marks from Student_1;

Output :

Roll_NoStudent_nameClassMarks
1Rahul10th68
2Rohan10th55
3Rohit9th43
1Rahul10th68
4Mohit10th47
3Rohit9th43
Union All

If you see carefully in the output table ; it will not eliminate the duplicate records. Just make sure that union all is faster than union operator. I hope you like this article. If you like this article or if you have any issues with the same kindly comment in comments section.

Amit S

Oracle Consultant with vast experience in Oracle BI and PL/SQL Development. Amiet is the admin head of this website who contributes by preparing tutorials and articles related to database technologies. He is responsible to manage the content and front-end of the website.

Recent Posts

What is Root Cause Analysis (RCA) With real examples

In my previous article I have given details about application support engineer day to day…

4 weeks ago

Application Support Engineer Day to day responsibilities

In my previous articles I have given the roles and responsibilities of L1,L2 and L3…

4 weeks ago

What is mean by SLA ( Service Level Agreement) with Examples?

In my previous articles i have given the hierarchy of production support in real company…

1 month ago

What is Production support Hierarchy in organization?

In this article i would like to provide information about production support organization structure or…

1 month ago

What are roles and responsibilities for L3 Support Engineer?

In my previous article I have given roles for L1 and L2 support engineer with…

1 month ago

What are roles and responsibilities of L2 Engineer?

I have started this new series of how to become application support engineer. This article…

1 month ago