SQL Server data set intersection, and, and differential set operations (INTERSECT,UNION,EXCEPT)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.