Home  >  Article  >  Database  >  How to use union in oracle

How to use union in oracle

下次还敢
下次还敢Original
2024-04-30 07:06:15781browse

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.

How to use union in oracle

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:

  • Only columns with the same number and data type can be merged.
  • UNION will delete duplicate records unless UNION ALL is used.
  • UNION ALL will combine all records, including duplicate records.

Example:

The following table contains two tables:

table1

##1John202Mary25
id name age

table2

##id34Using UNION, we can merge these two tables:
name job
Bob teacher
Alice student
<code>SELECT * FROM table1
UNION
SELECT id, name, NULL AS job FROM table2;</code>

Result:

id1234
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:length usage in oracleNext article:length usage in oracle