views:

3803

answers:

6

What is the difference between UNION and UNION ALL.

+6  A: 

The basic difference between UNION and UNION ALL is union operation eliminates the duplicated rows from the result set but union all returns all rows after joining.

from http://zengin.wordpress.com/2007/07/31/union-vs-union-all/

George Mauer
+31  A: 

UNION removes duplicate records (where all columns in the results are the same), UNION ALL does not.

Jim Harte
The implication of this, is that union is much less performant as it must scan the result for duplicates
Matthew Watson
+2  A: 

not sure that it matters which database

UNION and UNION ALL should work on all sql servers.

You should avoid of unnecessary UNIONs they are huge performance leak. As a rule of thumb use UNION ALL if you are not sure which to use.

Jakub Šturc
A: 

UNION does not support BLOB (or CLOB) column types, UNION ALL does.

Michiel Overeem
+4  A: 

UNION removes duplicates, whereas UNION ALL does not.

In order to remove duplicates the result set must be sorted, and this may have an impact on the performance of the UNION, depending on the volume of data being sorted, and the settings of various RDBMS parameters ( For Oracle PGA_AGGREGATE_TARGET with WORKAREA_SIZE_POLICY=AUTO or SORT_AREA_SIZE and SOR_AREA_RETAINED_SIZE if WORKAREA_SIZE_POLICY=MANUAL ).

Basically, the sort is faster if it can be carried out in memory, but the same caveat about the volume of data applies.

Of course, if you need data returned without duplicates then you must use UNION, depending on the source of your data.

I would have commented on the first post to qualify the "is much less performant" comment, but have insufficient reputation (points) to do so.

mathewbutler
A: 

union is used to select distinct values from two tables where as union all is used to select all values including duplicates from the tables