Basic Difference Between Union vs Union All Operator
Data Analytics

Basic Difference Between Union vs Union All Operator

Union: It is an Operator which is used to combine two or more tables.
It gives a unique result set and it will not allow duplicates.

Basic Conditions which need to be fulfilled by tables.

1. Both tables should have the same number of columns.
2. Both table columns should have the same data types.
3. Columns should be in the same order in each table.

Let's take an Example: 

We have 2 tables containing employee id and employee name and it has some duplicate values as well.

 

Table: Employee1

Employee IDEmployee First Name
11Bob
12Jerry
13John
14Alex
15Joe

 

Table: Employee2

Employee IDEmployee First Name
21Marry
12Jerry
23lily
13John
25Mat

 

Query:     Select  from Employee1 Union Select  from Employee2;

 

After Using Union Operator We will get only 8 records instead of 10 because we have 2 duplicates in Table 2.

Employee IDEmployee First Name
11Bob
12Jerry
13John
14Alex
15Joe
21Marry
23lily
25Mat

 

 


Union All: It is an operator which works the same as the Union operator and it also allows duplicate values.

 

Query:     Select  from Employee1 Union All Select  from Employee2;

 

After Using Union All Operator We will get 10 records which include 2 duplicate values.

 

Employee IDEmployee First Name
11Bob
12Jerry
13John
14Alex
15Joe
21Marry
12Jerry
23lily
13John
25Mat

 

 

Thanks for reading this and I hope you like it.

 

Mayank Agrawal
 

  • Mayank Agrawal
  • Dec, 27 2022

Add New Comments

Please login in order to make a comment.

Recent Comments

Be the first to start engaging with the bis blog.