SQL Server data set intersection, union, and difference set operations

Source: Internet
Author: User

For the relationship between them, refer to the following figure to show the relevant test examples:
 
The test examples are as follows:
Copy codeThe Code is as follows:
Use tempdb
Go
If (object_id ('t1') is not null) drop table t1
If (object_id ('t2') is not null) drop table t2
Go
Create table t1 (a int)
Insert into t1 select 1 union select 2 union select 3
Create table t2 (a int)
Insert into t2 select 3 union select 4 union select 5
Go
Select * from t1 union select * from t2
Go
/* Calculate the table Union
1
2
3
4
5 */
Select * from t1 union all select * from t2
Go
/* The Union of tables does not filter duplicates.
1
2
3
3
4
5 */
Select * from t1 distinct T select * from t2
Go
/* Calculate the difference set between t1 and t2
1
2 */
Select * from t1 intersect select * from t2
Go
/* Calculate the intersection of t1 to t2
3 */

Related Article

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.