Erik Darling hires the Pinkertons:
UNION and UNION ALL seem to get used with the same lack of discretion and testing as several other things in T-SQL: CTEs vs temp tables, temp tables vs. table variables, etc.
There are many times I’ve seen developers use UNION when result sets have no chance of being non-unique anyway, and many times I’ve seen them use UNION ALL when there would be a great benefit to discarding unnecessary duplicates.
Erik’s explanation goes about three steps beyond “UNION
is bad so always use UNION ALL
.” It’s a must-read for anybody who regularly writes T-SQL queries.