Oracle
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.
UNION
- Combines the results of two or more
SELECTstatements. - Eliminates duplicate rows from the final result.
- Requires each
SELECTstatement 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;- Combines the results of two or more
UNION ALL
- Combines the results of two or more
SELECTstatements. - Includes all rows, even duplicates.
- Generally performs better than
UNIONbecause it does not check for duplicates.
Example:
SELECT author_id, author_name FROM authors UNION ALL SELECT author_id, author_name FROM other_authors;- Combines the results of two or more
Key Differences
- Duplicates Handling:
UNION: Removes duplicates.UNION ALL: Keeps all duplicates.
- Performance:
UNION ALLis faster due to the lack of duplicate checking.
- Duplicates Handling:
Use Cases
- Use
UNIONwhen you need unique results. - Use
UNION ALLwhen you need all records, including duplicates, which is useful for counting purposes.
- Use
Best Practices
- Always ensure that the columns in each
SELECTstatement match in number and data type. - Prefer
UNION ALLoverUNIONwhen duplicates are not a concern, as it enhances performance.
- Always ensure that the columns in each
By understanding these concepts, beginners can effectively utilize UNION and UNION ALL to handle data from multiple sources in Oracle.
To gain complete access, login with gmail or outlook, no need of signup. click here
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 client table

Example 1 - UNION query
SELECT state
FROM org_employee
UNION
SELECT state
FROM org_client;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 - UNION ALL query
SELECT state
FROM org_employee
UNION ALL
SELECT state
FROM org_client;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



Comments Not Found