Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

What is the difference between UNION and UNION ALL in SQL?

What is the difference between UNION and UNION ALL?
I need more clarity in these two terms
by

2 Answers

Sonali7
The major difference between UNION ALL and UNION is that UNION ALL keeps all of the records from each of the original data sets including duplicates, whereas UNION only keeps the unique records.
Shahlar1vxp
The difference between UNION and UNION ALL is that UNION is known for removing duplicates while UNION ALL does not do that. For removing the duplicates, the result set should be first sorted and this might have an impact on the performance of UNION on the basis of the volume of data that is being sorted and also the settings of several RDBMS parameters. The sorting process is faster if it is carried out in memory but the same caution about the volume of data applies.
In case you need data to be returned without any duplicate, then you need to use UNION on the basis of the source of your data.
UNION does not support BLOB or CLOB column types in Oracle but UNION ALL does support. Like for example,
SELECT FROM mytable WHERE a=X UNION ALL SELECT FROM mytable WHERE b=Y AND a!=X
where AND a! = X part. This is faster than UNION.

Login / Signup to Answer the Question.