SQL Union Clause

Tips>>SQL

 

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.

 
News / Updates New version of cheque printing software is up. Can configure date in box. http://VirtualSplat.com/cp6.exe
E-mail : sales at virtualsplat dot com
Phone : +91 022 28067978