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?
The set operators are used to perform multiple set operations on tables in database. There are following 4 basic set operators used in SQL :
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.
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.
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_no | Student_name | Class | Marks |
1 | Rahul | 10th | 68 |
2 | Rohan | 10th | 55 |
3 | Rohit | 9th | 43 |
Second table
Table Name:Student_1
Roll_no | Student_name | Class | Marks |
1 | Rahul | 10th | 68 |
4 | Mohit | 10th | 47 |
3 | Rohit | 9th | 43 |
Query:
Select Roll_no,Student_name,Class,Marks from Student;
Union all
Select Roll_no,Student_name,Class,Marks from Student_1;
Output :
Roll_No | Student_name | Class | Marks |
1 | Rahul | 10th | 68 |
2 | Rohan | 10th | 55 |
3 | Rohit | 9th | 43 |
1 | Rahul | 10th | 68 |
4 | Mohit | 10th | 47 |
3 | Rohit | 9th | 43 |
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.
Introduction Cryptocurrencies took the world by storm, setting up a new financial system and breaking…
In my previous article I have given Top 20 technical support interview questions with its…
In my previous articles I have given 15 most asked desktop support interview questions with…
A business analyst is someone who is versed in processes of data analysis used for…
In my previous article I have already given top questions and answers for Desktop support…
In my previous article I have given unix production support interview questions and answers. In…