SQL Oracle PHP Java JSP JDBC MORE

The SQL UNION Operator


The UNION operator is used to find the result-set or combination of two or more tables.

Terms and Condition for using UNION:

  1. Each table used within UNION must have the same number of columns.
  2. The columns must have same data types.
  3. The columns in each table must be in the same order.

UNION Syntax

SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;

UNION ALL Syntax

The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL:

SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;

Example:

Select *
from Employee1;
ID Name Salary
1234 Divya 1000
2345 Anshu 2000
3456 Kalia 3000
Select *
from Employee2;
ID Name Salary City
5678 Pradeep 26000 gurgaon
7890 Mahesh 24500 Noida
7900 Raju 25600 Delhi
SELECT Name, Salary, City FROM Employee1
UNION ALL
SELECT Name, Salary, City FROM Employee2;