sql server fetch intersection, union, and difference set syntax

Here briefly summarize under intersected in SQL Server, and the poor set and set syntax.

Intersection: INTERSECT (applies to two result sets)

SELECT ID, NAME FROM YANGGB1
INTERSECT
SELECT ID, NAME FROM YANGGB2

Difference set: EXCEPT (applicable to two result sets)

SELECT ID, NAME FROM YANGGB1
EXCEPT
SELECT ID, NAME FROM YANGGB2

And set: UNION or UNION ALL (applicable to two result sets)

- not including duplicates, default sort for 
the SELECT ID, NAME the FROM YANGGB1
 the UNION 
the SELECT ID, NAME the FROM YANGGB2 

- including duplicates, without default sort 
the SELECT ID, NAME the FROM YANGGB1
 the UNION  ALL 
the SELECT ID, NAME the FROM YANGGB2

 

"Do a good, others are like people."

Guess you like

Origin www.cnblogs.com/yanggb/p/11819031.html