Javatpoint Logo
Javatpoint Logo

SQL UNION

UNION is an SQL operator which combines the result of two or more SELECT queries and provides the single set in the output.

Syntax of UNION in SQL:

The data type and the number of fields must be same for every SELECT statement connected with the UNION operator. The database system uses the UNION operator for removing the duplicate values from the combined result set.

Example of UNION operator in SQL

Let's create two different tables and insert records in both tables.

The following query creates the Old_Employee table with four fields:

The following query creates the New_Employee table with four fields:

The following INSERT query inserts the record of old employees into the Old_Employee table:

The following query shows the details of the Old_Employee table:


Employee_Id Employee_Name Emp_Age Emp_Salary
101 Akhil 28 25000
102 Abhay 27 26000
103 Sorya 26 29000
104 Abhishek 27 26000
105 Ritik 26 29000
106 Yash 29 28000

Table: Old_Employee

The following INSERT query inserts the record of new employees into the New_Employee table:

The following query shows the details of the New_Employee table:


Emp_Id Emp_Name Emp_Salary Emp_City
201 Jack 28 45000
202 Berry 29 35000
105 Ritik 26 29000
203 Shyam 27 26000
204 Ritika 28 38000
106 Yash 29 28000

Table: New_Employee

The following query shows all records of both tables in one table using the UNION operator:

Output:

Employee_Id Employee_Name Emp_Age Emp_Salary
101 Akhil 28 25000
102 Abhay 27 26000
103 Sorya 26 29000
104 Abhishek 27 26000
105 Ritik 26 29000
106 Yash 29 28000
201 Jack 28 45000
202 Berry 29 35000
203 Shyam 27 26000
204 Ritika 28 38000

Where Clause with the UNION operator

The WHERE clause can also be used with UNION operator to filter the records from one or both tables.

Syntax of UNION with WHERE clause

Example of UNION with WHERE Clause

The following query shows those records of employees from the above tables whose salary is greater than and equal to 29000:

Output:

Employee_Id Employee_Name Emp_Age Emp_Salary
103 Sorya 26 29000
105 Ritik 26 29000
201 Jack 28 45000
202 Berry 29 35000
204 Ritika 28 38000

Union ALL Operator in SQL

The SQL Union ALL Operator is same as the UNION operator, but the only difference is that UNION ALL operator also shows the common rows in the result.

Syntax of UNION ALL Set operator:

Example of UNION ALL

Let's create two different tables and insert records in both tables.

The following query creates the Passed_Students table with four fields:

The following query creates the New_Students table with four fields:

The following INSERT query inserts the record of passed students into the Passed_Students table:

The following query shows the details of the Passed_Students table:


Student_Id Student_Name Student_Age Student_Marks
101 Akhil 28 95
102 Abhay 27 86
103 Sorya 26 79
104 Abhishek 27 66
105 Ritik 26 79
106 Yash 29 88

Table: Passed_Students

The following INSERT query inserts the record of new students into the New_Students table:

The following query shows the details of the New_Students table:


Student_Id Student_Name Student_Age Student_Marks
201 Jack 28 77
202 Berry 29 66
105 Ritik 26 82
203 Shyam 27 70
204 Ritika 28 99
106 Yash 29 86

Table: New_Students

The following query shows all duplicate and unique records from both tables:

Output:

Student_Id Student_Name Student_Age Student_Marks
101 Akhil 28 95
102 Abhay 27 86
103 Sorya 26 79
104 Abhishek 27 66
105 Ritik 26 79
106 Yash 29 88
201 Jack 28 77
202 Berry 29 68
105 Ritik 26 82
203 Shyam 27 70
204 Ritika 28 99
106 Yash 29 86






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA