Oracle Union | union - oracle tutorial - sql tutorial
![Oracle union](https://wikitechy.com/tutorials/oracle/img/oracle-images/oracle-union.gif)
Learn oracle - oracle tutorial - Oracle union - oracle examples - oracle programs
What is Oracle UNION ?
- The Oracle UNION operator is used to combine the result sets of 2 or more Oracle SELECT statements.
- It removes duplicate rows between the various SELECT statements.
![](https://wikitechy.com/tutorials/oracle/img\oracle-images\union.gif)
Syntax:
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
UNION
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];
click below button to copy the code. By - oracle tutorial - team
- Note : Each SELECT statement within the UNION operator must have the same number of fields in the result sets with similar data types.
![Oracle union query](https://wikitechy.com/tutorials/oracle/img\oracle-images\sql-union-query-example.png)
Oracle Union Query
Sample Database:
- We have a table called wikitechy_employee with four fields (id, name, position, and city). It contains the following data:
![oracle union table](https://wikitechy.com/tutorials/oracle/img\oracle-images\oracle-union-table1.png)
- We have another table called orders with three fields (id, customer_name, and orderid).
![oracle union table](https://wikitechy.com/tutorials/oracle/img\oracle-images\oracle-union-table-2.png)
oracle tutorial , sql tutorial , sql , pl sql tutorial , oracle , pl sql , plsql
Example:
SELECT City FROM wikitechy_employee
UNION
SELECT City FROM orders
ORDER BY City;
click below button to copy the code. By - oracle tutorial - team
Note:
- There must be same number of expressions in both SELECT statements.
Union:
![union output](https://wikitechy.com/tutorials/oracle/img\oracle-images\union-output.png)
- Select statement is used to select city column from the wikitechy_employee table and similarly city column from orders table.
- Union Keyword is used to combines distinct values in city column from both tables.
- If several employee and customers share same city, each city will only be selected.
- Note: Use Union All keyword instead of union to select all the duplicate values.
Union UnionAll Except Intersect
![Union UnionAll Except Intersect](https://wikitechy.com/tutorials/oracle/img\oracle-images\union-unionall-except-intersect.jpg)