EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Oracle Tutorial Oracle UNION ALL
Secondary Sidebar
Oracle Tutorial
  • Oracle SET Operators
    • UNION in Oracle
    • Oracle UNION ALL
    • INTERSECT in Oracle
    • MINUS in Oracle
  • Basic
    • Oracle Marketing Cloud
    • What is Oracle?
    • Career in Oracle
    • How to Install Oracle
    • Oracle Versions
    • What Is Oracle Database
    • Oracle Data Warehousing
    • Oracle Warehouse Builder
    • Career In Oracle Database Administrator
    • Career In Oracle DBA
    • What is Oracle RAC
    • Oracle DBA
    • Oracle? Vanderbilt
    • What is RMAN Oracle
    • Oracle Database Administration
    • Oracle Operators
    • Oracle Constraints
    • Oracle number
    • Oracle Data Types
    • Oracle UNIQUE Constraint
    • Oracle Check Constraint
  • Joins
    • Joins in Oracle
    • Inner Join in Oracle
    • Oracle Cross Join
    • Left Join in Oracle
    • OUTER Join in Oracle
    • Oracle Full Outer Join
    • Natural Join in Oracle
    • Oracle Self Join
    • Oracle hash join
    • Oracle? Update with Join
  • Advanced
    • Timestamp to Date in Oracle
    • Oracle Golden Gate
    • Oracle Virtual Machine
    • Oracle Describe Table
    • Oracle Clauses
    • Oracle Having Clause
    • Oracle?Primavera
    • Oracle FOREIGN Key
    • PIVOT in Oracle
    • Oracle Alter Table
    • Oracle Queries
    • Oracle Views
    • Oracle Window Functions
    • Oracle String Functions
    • Oracle Date Functions
    • Oracle Analytic Functions
    • Oracle Aggregate Functions
    • Select in Oracle
    • INSERT in Oracle
    • DISTINCT in Oracle
    • Function in Oracle
    • Oracle GROUP_CONCAT
    • Oracle INSTR()
    • Oracle CONVERT
    • Oracle LENGTH()
    • Oracle EXISTS
    • Oracle REPLACE()
    • Oracle MERGE
    • Oracle LEAD()
    • Oracle EXTRACT()
    • Oracle LISTAGG()
    • Oracle SYS_CONTEXT()
    • Oracle COALESCE
    • Oracle NVL()
    • Oracle SYSDATE()
    • Oracle?Date Format
    • Oracle SYS_GUID()
    • Oracle WILDCARDS
    • Oracle Synonyms
    • Oracle Subquery
    • BETWEEN in Oracle
    • FETCH in Oracle
    • Oracle Index
    • Oracle Function-based Index
    • Oracle UNIQUE Index
    • Oracle Bitmap Index
    • Oracle Column
    • Oracle Triggers
    • Oracle Procedures
    • Sample Database for Oracle
    • Oracle LIKE Operator
    • ORDER BY in Oracle
    • Oracle ORDER BY DESC
    • GROUP BY in Oracle
    • Oracle GROUP BY HAVING
    • Oracle Aliases
    • Table in Oracle
    • Oracle Temporary Table
    • Oracle? Table Partition
    • Oracle rename table
    • Oracle CTE
    • Cursor in Oracle
    • Oracle LOCK TABLE
    • Oracle Tablespace
    • Oracle CARDINALITY
    • Oracle REGEXP
    • Oracle REGEXP_REPLACE
    • Oracle to_date
    • JSON in Oracle
    • Oracle COMMIT
    • Oracle GRANT
    • Oracle MD5
    • Oracle ROLLBACK
    • Oracle Users
    • Oracle TIMESTAMP
    • IF THEN ELSE in Oracle
    • Oracle While Loop
    • Oracle Clone Database
    • Oracle Backup Database
    • Oracle? XML
    • Oracle XMLAGG
    • Oracle XMLTABLE
    • Oracle Performance Tuning
    • Oracle B Tree Index
    • Oracle fusion
    • Oracle ebs
    • Oracle GRC
    • Oracle ERP
    • Oracle ASM
    • Oracle Cloud
    • Oracle HCM Cloud
    • Oracle Integration Cloud
    • Oracle Jinitiator
    • Oracle pathfinder
    • Oracle VirtualBox
    • Oracle Weblogic Server
    • Oracle decode
    • Oracle Exadata
    • Oracle ZFS
    • Oracle? utilities
    • JDBC Driver for Oracle
    • Oracle? DBA Versions
    • Oracle DBA Salary
  • Interview Questions
    • Oracle Interview Questions
    • Oracle Apps Interview Questions
    • Oracle Apps Technical Interview Questions
    • Oracle Database Interview Questions
    • Oracle Forms Interview Questions
    • Oracle PL/SQL Interview Questions
    • Oracle RAC Interview Questions
    • Oracle SOA Interview Questions

Related Courses

Oracle Course Training

Oracle DBA Certification Course

MongoDB Certification Training

Oracle UNION ALL

By Priya PedamkarPriya Pedamkar

Oracle UNION ALL

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.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (85,992 ratings)
  • 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:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Dataset 1

Dataset 2

1

2

2

3

3

4

1

5

Oracle UNION ALL

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:

Oracle UNION ALL example

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:

Oracle UNION ALL basic

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:

Oracle UNION ALL eg1

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:

Oracle UNION ALL eg2

The first SELECT statement fetched records as shown below.

Oracle UNION ALL eg2.1

And second SELECT statement fetched records as shown below.

Oracle UNION ALL eg2.2

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:

Oracle UNION ALL eg3

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:

Invalid Column name output

As column name “Dname” does not exist in Emp Table, hence the output shows the invalid identifier.

NOTE: This example proves that each SELECT statement creates a separate result set.

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:

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:

output throws an ERROR

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:

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 –

  1. Examples to Implement Cross Join in Oracle
  2. Rules and Restrictions of Oracle UNION Operator
  3. How to Use DISTINCT in Oracle?
  4. Complete Guide to PostgreSQL Cross Join
Popular Course in this category
Oracle Training (14 Courses, 8+ Projects)
  14 Online Courses |  8 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle DBA Database Management System Training (2 Courses)4.9
All in One Financial Analyst Bundle- 250+ Courses, 40+ Projects4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more