SQL Union Clause

Tips>>SQL

MAKE FULL WEB APPLICATION WITH JUST SQL KNOWLEDGE? CLICK HERE

 

To combine the data from two tables into a single output, we use the UNION clause in the SELECT statement. Union gives all the records from both the queries after eliminating the duplicate records. Each SQL statement within the UNION ALL query must have the same number of fields in the result sets with similar data types.

The syntax of UNION statement is :-

SQL Statement 1

UNION

SQL Statement 2

 

 

Employees Mumbai :-

Employee_ID
E_Name
01
Verma, Mihir
02
Shah, Parin
03
Thakkar, Rahul
04
Sagar, Manan

 

Employees Banglore :-

Employee_ID
E_Name
01
Shah, Pavan
02
Karia, Anish
03
Verma, Mihir
04
Bhayani, Nirav

Using the UNION Command

 

Example:-
List all different employee names in Mumbai and Banglore :-

SELECT E_Name FROM Employees_Mumbai

UNION SELECT E_Name FROM Employees_Banglore

RESULT:-

NAME
Verma, Mihir
Shah,Parin
Thakkar, Rahul
Sagar, Manan
Shah, Pavan
Karia, Anish
Bhayani, Nirav

Note: This command cannot be used to list all employees in Mumbai and Banglore. In the example above we have one employees with equal names, and only one of them is listed. The UNION command only selects distinct values.

If you don't find what you are looking for. Please click here to submit your query, our experts will reply soon.

 
E-mail : sales@virtualsplat.com
Phone : +91-9892413501

Whatsapp Icon +91-9967648641

Whatsapp Icon +91-9967648641