What is the difference between union and union all in SQL?

Viewed 333

Difference between union and union all?

1 Answers

UNION ALL keeps all of the records from each of the original data sets, UNION removes any duplicate records. Union is more expensive than Union All since Union needs to eliminate duplicates form the resultset.

In SQL Server UNION first performs a sorting operation and eliminates of the records that are duplicated across all columns before finally returning the combined data set.

If you are interested to know further please have a look a below link:

https://dataschool.com/learn-sql/what-is-the-difference-between-union-and-union-all/#:~:text=records%2C%20including%20duplicates-,UNION%20ALL%20Difference,returning%20the%20combined%20data%20set.

Related