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

The Essential Guide to Cryptocurrency Exchange Platform Development for Beginners and Beyond

Introduction Cryptocurrencies took the world by storm, setting up a new financial system and breaking…

12 months ago

Top 20 System Administrator Interview Questions and answers

In my previous article I have given Top 20 technical support interview questions with its…

1 year ago

Desktop Support Scenario Based Interview Questions

In my previous articles I have given 15 most asked desktop support interview questions with…

1 year ago

How Do Business Analysts Use SQL for Their Needs?

A business analyst is someone who is versed in processes of data analysis used for…

1 year ago

Top 15 Control-M Interview Questions with Answers

In my previous article I have already given top questions and answers for Desktop support…

1 year ago

Top 20 SQL Interview Questions for Production Support

In my previous article I have given unix production support interview questions and answers. In…

1 year ago