;

SQL UNION


In this tutorial, we will learn how to use UNION or UNION ALL opertator and combine the result set of two or more SELECT statement into a single result set using SQL.

SQL UNION Operator

  • SQL UNION Clause/Operator is used to combine the result set of two or more SELECT statements and only returned unique records/rows.
  • For using UNION Clause/Operator, there are some basic rules as follow:
  • Each SELECT statement/queries within UNION Clause must have the same number of columns.
  • Columns must also have similar datatypes.
  • The order of the columns must be the same in all SELECT statements/queries.
Syntax - UNION Operator
SELECT column1,column2,...columnN FROM tableName1
UNION
SELECT column1,column2,...columnN FROM tableName2;

SQL UNION ALL

  • SQL UNION ALL Clause/Operator is used to combine the result set of two or more SELECT statements and returned all the duplicate records/rows also.
  • The same rules that apply to the UNION clause/operator will apply to the UNION ALL clause/operator.
Syntax - UNION ALL Operator
SELECT column1,column2,...columnN FROM tableName1
UNION ALL
SELECT column1,column2,...columnN FROM tableName2;

UNION vs UNION ALL

UNION AND UNION ALL Both used to combine the result-set of two or more SELECT statements. The Only difference between both UNION and UNION ALL is that UNION  returns distinct records while UNION ALL returns all the records.

Example:

Let us consider these two tables "Customer" and "Suppliers" for records.

Table Name: Customer

ID FirstName LastName City Country Phone Pincode
1 Sourabh Chauhan Delhi India 8899885522 110044
2 Rakesh Saini Faridabad India 5878458595 121001
3 Kapil Saini Noida India 5878987885 203103
4 Hemant Chauhan Delhi India 2145622222 110044
5 Vivek Kumar Delhi India 1523654789 110096
6 Sammy Brown New York US 1325647899 11001
7 Tom Hardy LA US 1215466456 10001

Table Name: Suppliers

ID FirstName LastName City Country Phone Pincode
1 Chris Pratt New York US 1199885522 11001
2 Sarah Jane Sydney Australia 3878458595 2000
3 Kapil Chauhan Agra India 5878987885 223007
4 Hemant Chaurasia Delhi India 2145622222 110077
5 Vivek Ojha Delhi India 1523654789 110036
6 Sameer Kalia New York US 1325647899 11001
7 Tom Hilfiger LA US 1215466456 10001

Example of UNION Clause/Operator

Select the records(ID, FirstName, City) from both the tables (Customer and Suppliers) using UNION clause,  a query will be:-

Example - UNION Clause/Operator
SELECT ID,FirstName,City FROM Customer
UNION
select ID,FirstName,City FROM Suppliers;
Output
ID Name City
1 Chris New York
1 Sourabh Delhi
2 Rakesh Faridabad
2 Sarah Sydney
3 Kapil Agra
3 Kapil Noida
4 Hemant Delhi
5 Vivek Delhi
6 Sameer New York
6 Sammy New York
7 Tom LA

Example of UNION ALL Clause/Operator

Select the records(ID, FirstName, City) from both the tables (Customer and Suppliers) using UNION ALL clause,  a query will be:

Syntax - of SELECT Statement
SELECT ID,FirstName,City FROM Customer
UNION ALL
select ID,FirstName,City FROM Suppliers;
Syntax - SELECT Statement
ID Name City
1 Sourabh Delhi
2 Rakesh Faridabad
3 Kapil Noida
4 Hemant Delhi
5 Vivek Delhi
6 Sammy New York
7 Tom LA
1 Chris New York
2 Sarah Sydney
3 Kapil Agra
4 Hemant Delhi
5 Vivek Delhi
6 Sameer New York
7 Tom LA