The usage of UNION in Oracle is to merge multiple query result sets with the same structure into a single result set. This operator removes duplicate records unless UNION ALL is used, which merges all records, including duplicates.
UNION usage in Oracle
UNION is used in Oracle to combine multiple queries SQL operator that combines result sets into a single result set. It is used to merge tables or query results that have the same structure (column names and data types).
Syntax:
<code>SELECT ... UNION SELECT ... UNION ...</code>
Usage:
Merge tables with the same structure:
<code>SELECT * FROM table1 UNION SELECT * FROM table2;</code>
Merge different queries:
<code>SELECT name, age FROM students UNION SELECT name, NULL AS age FROM teachers;</code>
Note:
Example:
The following table contains two tables:
table1
id | name | age |
---|---|---|
John | 20 | |
Mary | 25 |
table2
name | job | |
---|---|---|
Bob | teacher | |
Alice | student |
<code>SELECT * FROM table1 UNION SELECT id, name, NULL AS job FROM table2;</code>
Result:
name | age | job | |
---|---|---|---|
John | 20 | null | |
Mary | 25 | null | |
Bob | null | teacher | |
Alice | null | student |
The above is the detailed content of How to use union in oracle. For more information, please follow other related articles on the PHP Chinese website!