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_IdEmployee_NameEmp_AgeEmp_Salary
101Akhil2825000
102Abhay2726000
103Sorya2629000
104Abhishek2726000
105Ritik2629000
106Yash2928000

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_IdEmp_NameEmp_SalaryEmp_City
201Jack2845000
202Berry2935000
105Ritik2629000
203Shyam2726000
204Ritika2838000
106Yash2928000

Table: New_Employee

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

Output:

Employee_IdEmployee_NameEmp_AgeEmp_Salary
101Akhil2825000
102Abhay2726000
103Sorya2629000
104Abhishek2726000
105Ritik2629000
106Yash2928000
201Jack2845000
202Berry2935000
203Shyam2726000
204Ritika2838000

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_IdEmployee_NameEmp_AgeEmp_Salary
103Sorya2629000
105Ritik2629000
201Jack2845000
202Berry2935000
204Ritika2838000

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_IdStudent_NameStudent_AgeStudent_Marks
101Akhil2895
102Abhay2786
103Sorya2679
104Abhishek2766
105Ritik2679
106Yash2988

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_IdStudent_NameStudent_AgeStudent_Marks
201Jack2877
202Berry2966
105Ritik2682
203Shyam2770
204Ritika2899
106Yash2986

Table: New_Students

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

Output:

Student_IdStudent_NameStudent_AgeStudent_Marks
101Akhil2895
102Abhay2786
103Sorya2679
104Abhishek2766
105Ritik2679
106Yash2988
201Jack2877
202Berry2968
105Ritik2682
203Shyam2770
204Ritika2899
106Yash2986





Latest Courses