Set operators combine results from two or more queries into a single result set. There are 3 Transact-SQL set operators available in SQL Server namely, UNION, EXCEPT and INTERSECT. The following are basic rules for combining the result sets of two queries by using the UNION, EXCEPT and INTERSECT set operators:
- The number and the order of the columns must be the same in all queries.
- The data types must be compatible.
The UNION set operator combines the results of two or more queries into a single result set that includes all the rows that belong to all queries in the union. The UNION set operator is different from using joins that combine columns from two tables.
The EXCEPT set operator returns any distinct values from the left query that are not also found on the right query.
The INTERSECT set operator returns any distinct values that are returned by both the query on the left and right sides of the INTERSECT operand.