Updated March 24, 2023
What is Oracle UNION ALL Operator?
The Oracle UNION ALL operator combines the result sets of two or more SELECT statements into one result set and keeps the duplicate record(s). In simple words, UNION ALL operator returns all records of SELECT statements and does not eliminate duplicate records.
- UNION ALL operator can be applied on two or more than two SELECT statements.
- UNION ALL operator is often called VERTICAL JOIN, as the result combines data from two or more SELECT statements based on column instead of rows.
- The result of each SELECT statement can be treated as a set and UNION ALL operator applies on the sets and combines the result sets to get the one final result or a single set with all the record(s) that exists in each SELECT statement.
Pictorial Representation:
Dataset 1 |
Dataset 2 |
1 |
2 |
2 |
3 |
3 |
4 |
1 |
5 |
Explanation: The UNION ALL query returns all the records that are in the both (red & black) outline area and keeps the duplicate records in the final result set.
Points of Concentration
- The queries are all executed independently, but their output is merged.
- Only the final query ends with a semicolon ‘;’
Syntax:
SELECT col_1, col_2, ..., col_n FROM TableName WHERE condition(s)
UNION ALL
SELECT col_1, col_2, ..., col_n FROM TableName WHERE condition(s);
Description:
- Col_1/2/n: The column(s) or calculation as per your requirement.
- Table Name: As per your requirement
- WHERE: It’s an optional, depends on your requirement
Example:
SELECT Empno, Ename, Job, Deptno FROM Emp WHERE Deptno=10
UNION ALL
SELECT Empno, Ename, Job, Deptno FROM Emp WHERE Deptno=30;
Output:
Rules and Restrictions of Oracle UNION ALL
The following are the important rules and restrictions of UNION ALL operators as listed below.
- The result sets of the queries must have the same number of columns.
- The data type of each column in the second result set must match the data type of its corresponding column in the first result set.
- The final result of the UNION ALL operation cannot be ordered if the SELECT statement does not contain an ORDER BY clause.
- To sort the final result set, the SELECT statement may contain an ORDER BY clause but the last SELECT statement only can contain the ORDER BY clause.
Examples of Oracle UNION ALL Operator
In this section, we’ll see the implementation of UNION ALL operator and its behavior. For that, we will use the below sample table (Emp) with 14 records to understand the UNION ALL operator behavior.
Query:
SELECT * from Emp ;
Output:
Example #1 – Without Any Condition
In this example, UNION ALL operator returns 28 records because UNION ALL operator simply merged the second result set with the first result set and each result set has had 14 records.
Query:
SELECT Empno, Ename, job, Deptno FROM Emp
UNION ALL
SELECT Empno, Ename, job, Deptno FROM Emp;
Output:
Example #2 – With WHERE Clause
In this example, each SELECT statement generates a separate result set based on condition and UNION ALL operator merges both the sets and shows a final result with 10 records.
Query:
SELECT Empno, Ename, Job, Deptno FROM Emp WHERE Job =’SALESMAN’
UNION ALL
SELECT Empno, Ename, Job, Deptno FROM Emp WHERE Deptno = 30;
Output:
The first SELECT statement fetched records as shown below.
And second SELECT statement fetched records as shown below.
And it clearly shows that the records of the second set is combining with the first set records, that’s why the final result shows 10 rows output with duplicate records.
Example #3 – With Different Number of Columns
Here we are using a different number of columns, let’s see the query below.
Query:
SELECT Empno, Ename, Deptno FROM Emp WHERE Deptno = 10
UNION ALL
SELECT Ename, Deptno FROM Emp WHERE Deptno = 30;
Output:
We can see we are getting the error instead of output because UNION ALL operator rules and restrictions say that the result sets of all SELECT statements must have the same NUMBER of columns. But in this example, the first SELECT statement contains three columns but the second SELECT statement contains two columns.
Example #4 – With Invalid Column Name
In this example, we are trying to search for a column that does not exist in a table.
Query:
SELECT Dname, Ename, Deptno FROM Emp WHERE Deptno = 10
UNION ALL
SELECT Ename, Deptno FROM Emp WHERE Deptno = 30;
Output:
As column name “Dname” does not exist in Emp Table, hence the output shows the invalid identifier.
Example #5 – With Mismatch Data Type
In this example, column numbers are the same in both the result sets but the data type is mismatching.
Query:
SELECT Ename, Hiredate, Deptno FROM Emp WHERE Deptno =10
UNION ALL
SELECT Ename, Job, Deptno FROM Emp WHERE Deptno = 30;
Output:
As UNION ALL operator rules and restrictions says that the data type of each column in the second result set must match the data type of its corresponding column in the first result set. The first SELECT statement result set consists of column “Hiredate” which is DATE data type but the second result set consists column “Job” which is a VARCHAR2 data type which is not matching, so the OUTPUT shows data type error.
Example #6 – With ORDER BY Clause
In this example, output throws an ERROR even if the column number and data type criteria is matching with both the result sets.
Query:
SELECT Empno, Ename, Deptno FROM Emp WHERE Job =’SALESMAN’
ORDER BY Empno
UNION ALL
SELECT Empno, Ename, Deptno FROM Emp WHERE Deptno = 30;
Output:
In this example, the ORDER BY clause used by the first SELECT statement to sort the result set. But we can only use the ORDER BY clause in the last SELECT statement. The following is the correct way to apply for the ORDER BY clause as shown below.
Query:
SELECT Empno, Ename, Job, Deptno FROM Emp WHERE Job =’SALESMAN’
UNION ALL
SELECT Empno, Ename, Job, Deptno FROM Emp WHERE Deptno = 30
ORDER BY 1;
Output:
In this example, the ORDER BY clause has been used by the last SELECT statement to sort the result without any error.
Tips:
- In the ORDER BY clause, column position number can be also used instead of a column name.
- In some cases UNION ALL operation is more costly than other operations (join, subquery, etc.)
- UNION ALL gives better performance as compare to UNION ALL because UNION has an extra overhead to sort the final result set by default but it’s not applicable for UNION ALL.
Conclusion
UNION ALL Operator performs VERTICAL Join and returns all record(s) that exist in the result sets. To get the all record(s), that is/are existing in the result set, UNION ALL operator can be used but column(s) number and data type must be the same.
Recommended Articles
This is a guide to Oracle UNION ALL. Here we discuss the syntax and various examples of Oracle UNION ALL with code implementation. You can also go through our suggested articles to learn more –