Set and Union Operations


When you are performing a set operation such as a UNION, you need to abide by certain rules. The types returned from the two sets must be compatible. There cannot be an ORDER BY in either of the queries. If you do have different column names in the queries, the result will get the names from the first set.

The UNION operator implicitly does a DISTINCT on the results. That takes up some extra time. If you don’t mind duplicates in the data, you can instead use the UNION ALL operator that allows duplicates.
The INTERSECT operator returns items that are in common between the two sides. This will also implicitly perform a DISTINCT and not return any duplicates. The EXCCEPT operator returns a difference. It gives you items in the first query that do not appear in the second query results.