SQL Server uses intersect,union,except and three keywords to correspond to three sets of intersection, and, and difference.
Their correspondence can refer to the following illustration
Test Example:
Construct A, a, two data set
A:1,2,3,4B:1,2,5 withA as (SELECT '1'TNOUNION All SELECT '2' UNION All SELECT '3' UNION All SELECT '4'), B as(SELECT '1'TNOUNION All SELECT '2' UNION All SELECT '5')
Query Example:
1 Union collection and filtering of duplicate data
-- 1 Union collection and filtering of duplicate data -- results show: 1,2,3,4,5 SELECT * from A UNION SELECT * from B
2 Union All collection does not filter duplicate data
-- 2 Union All collection does not filter duplicate data -- results show: 1,2,3,4,1,2,5 SELECT * from A UNION All SELECT * from B
3 Intersect intersection (data in two tables)
-- 3 Intersect intersection -- The results show that SELECT * from A Intersect SELECT * from B
4 except set (Take a A-B record)
-- 4 except difference set -- results show: 3,4 SELECT * from A except SELECT * from B
SQL Server data set intersection, and, and differential set operations (INTERSECT,UNION,EXCEPT)