Oracle

Chapter 7 - DQL (Data Query Language)

UNION and UNION ALL

In Oracle, the UNION and UNION ALL operators are used to combine the results of two or more SELECT statements. They allow you to retrieve data from multiple tables or queries and present it as a single result set. While both operators merge results, they differ in how they handle duplicates. Understanding when to use each operator can help optimize query performance and ensure data integrity.

  1. UNION

    • Combines the results of two or more SELECT statements.
    • Eliminates duplicate rows from the final result.
    • Requires each SELECT statement to have the same number of columns with compatible data types.

    Example:

    SELECT author_id, author_name FROM authors
    UNION
    SELECT author_id, author_name FROM other_authors;
    
  2. UNION ALL

    • Combines the results of two or more SELECT statements.
    • Includes all rows, even duplicates.
    • Generally performs better than UNION because it does not check for duplicates.

    Example:

    SELECT author_id, author_name FROM authors
    UNION ALL
    SELECT author_id, author_name FROM other_authors;
    
  3. Key Differences

    • Duplicates Handling:
      • UNION: Removes duplicates.
      • UNION ALL: Keeps all duplicates.
    • Performance:
      • UNION ALL is faster due to the lack of duplicate checking.
  4. Use Cases

    • Use UNION when you need unique results.
    • Use UNION ALL when you need all records, including duplicates, which is useful for counting purposes.
  5. Best Practices

    • Always ensure that the columns in each SELECT statement match in number and data type.
    • Prefer UNION ALL over UNION when duplicates are not a concern, as it enhances performance.

By understanding these concepts, beginners can effectively utilize UNION and UNION ALL to handle data from multiple sources in Oracle.

Tansy SQL Course | UNION and UNION ALL | Chapter 7 | Lesson 27 - Video Thumbnail

TEST CODE

In Oracle, to retrieve distinct values from the "state" column in both the "org_employee" and "org_client" tables using the UNION operator, you can use the following query:

SELECT state
FROM org_employee
UNION
SELECT state
FROM org_client;

In Oracle, to retrieve all values from the "state" column in both the "org_employee" and "org_client" tables using the UNION ALL operator, you can use the following query:

SELECT state
FROM org_employee
UNION ALL
SELECT state
FROM org_client;

Example 1:

Let's delve into SQL's UNION and UNION ALL operators. This example will showcase the results obtained from both UNION and UNION ALL, simultaneously illustrating the distinctions between them.

Example 1 - Raw data from employee table

Example 1 Raw data from employee table

Example 1 - Raw data from client table

Example 1 Raw data from client table

Example 1 - UNION query

SELECT state
FROM org_employee
UNION
SELECT state
FROM org_client;

Example 1 - Query data mapping for UNION

Example 1 Query data mapping for UNION

In the given image, the green hue indicates the data that has been chosen or meets the criteria specified in our query. It's evident that only unique values from both the employee and client tables are selected as the output.

Example 1 - Query Output for UNION

Example 1 Query Output for UNION

Example 1 - UNION ALL query

SELECT state
FROM org_employee
UNION ALL
SELECT state
FROM org_client;

Example 1 - Query data mapping for UNION ALL

Example 1 Query data mapping for UNION ALL

In the given image, the green hue indicates the data that has been chosen or meets the criteria specified in our query. It's evident that all values both the employee and client tables are selected as the output.

Example 1 - Query Output for UNION ALL

Example 1 Query Output for UNION ALL
Comments(0 comments)

Comments Not Found