You may be familiar with the UNION operator in Oracle SQL, but are aware of the UNION ALL operator?

Perhaps you will be surprised to hear that more often than not when you write UNION, what you meant to use is UNION ALL.

The UNION operator is used to combine result sets from multiple SELECT statements into a single result set. However, it eliminates duplicates in process.

In order for the database to do this the database will need to join the two result sets, sort them, find the duplicates and eliminate them.

Hey wait a minute, sort the results? Yes, the most efficient way to eliminate duplicates is to order the results and then find the duplicate entries. To make matters worse, if you have a “large” amount of data, this sort operation may not be able to be performed in memory (which would be the most efficient approach).

UNION ALL on the other hand will combine the result sets as they are. It will return all rows.

There’s clearly a use case for both scenarios. However, I would submit to you, more often than not, what we really want is to combine all the rows.

Be aware, that when using UNION you’re asking the database to do more work, make sure that’s what you really want.

I know that if you search for UNION vs UNION ALL you’ll find a gazillion results. But hey, I keep seeing UNION misused, so maybe we need one more blog about it.


Hi, I'm Jorge Rimblas. Father, husband, photographer, Oraclenerd, Oracle APEX expert, Oracle ACE, coffee lover, car guy, gadget addict, etc... I'm a Senior APEX Consultant with Insum Solutions, a consulting firm specialized in Oracle databases and the APEX development tool. I have worked with Oracle since 1995 and done eBusiness Suite implementations and customizations. Nowadays I specialize almost exclusively in Oracle APEX.

Posted in Oracle
2 comments on “UNION or UNION ALL
  1. Francesco says:

    Excellent Jorge

I love comments, write me a line