Use of the Union and Union ALL of SQL statements _mssql

Source: Internet
Author: User

The Union and JOIN statements in SQL are very similar, but the union has a constraint: two SQL statements produce a field that needs to be the same type of data, and the final result will see a different data value.

Format:

[SQL Statement 1]
UNION
[SQL Statement 2]

For example:

Table Store_info

and table Internet_sales

Use the Union statement:

Copy Code code as follows:
SELECT Date from Store_info
UNION
SELECT Date from Internet_sales

Results:

Or:

Copy Code code as follows:
SELECT Date,sales from Store_info
UNION
SELECT Date,sales from Internet_sales

Results:

Finally, the union all and the Union are basically the same, except that the result is that it contains duplicate data.

Such as:

Copy Code code as follows:
SELECT Date from Store_info
UNION All
SELECT Date from Internet_sales

Results:

The above is the SQL statement of the Union and the use of union all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.

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.