Go to Top

SQL Tech Tip: UNION ALL vs. UNION?

Cognos SQL Tech Tip

When doing a union query, is it better to use UNION ALL versus UNION? The UNION will try and eliminate duplicate rows between the two queries and essentially does a select distinct on the entire query. Most of the time when doing a union, users build the queries so that each query returns unique results and therefore they don’t need UNION. So, if you have a slow union query, try changing to UNION ALL. Bottom line: UNION ALL does not remove duplicates, therefore it is faster than UNION.

Thanks to our two expert consultants Brian and Emily for providing this great SQL tech tip!

, ,