Difference between UNION and UNION ALL is one of the tricky SQL question, especially for developers, who has not used this useful keyword ever. Since UNION clause is not as common as SELECT clause in SQL, it's usually asked in telephonic round of programming interviews to check whether candidate is comfortable with SQL or not. It's in the same league of questions like
clustered vs non-clustered index or
primary vs unique key.
UNION is very different than other SQL commands because it operates on data rather than columns. Anyway, answer of this question is simple, though both
UNION and
UNION ALL are used to combine result of two separate SQL queries on same or different table, UNION does not keep duplicate record (a row is considered duplicate if value of all columns are same), while UNION ALL does. Since you mostly don't want duplicate rows,
UNION is preferred over
UNION ALL in reporting and application development. By the way, you should keep in mind that
UNION ALL performance better than UNION because it doesn't have to remove duplicate, so no extra work. This keyword is very well supported by all major database e.g. Oracle, Microsoft SQL Server, MySQL and PostgreSQL. Another thing to keep in mind is amount data returned by
UNION ALL; if your database server is quite far away and you have limited bandwidth,
UNION ALL may appear slower than
UNION because of number of duplicates it returned. Cost of transferring
duplicate row can exceed the query execution benefits in many cases. We will see couple of
examples UNION and UNION ALL in SQL, but before that few things to keep in mind. In order to combine results of two queries, they must contain same number of columns. For example if one query contains 3 columns and other contains 4 columns then you can not use
UNION or
UNION ALL. This is because a row will only be considered duplicated when all column will have same value, irrespective of name of columns itself.