sql--Union query "union"

Source: Internet
Author: User

The set operator is operated on two sets, two sets must have the same number of columns, the column has the same data type (at least implicitly convertible), and the final output of the collection's column name is determined by the column name of the first collection (which can be used to concatenate multiple results).

Note: Union "union" is not the same as join.

Union: Combines multiple result sets into a single result set. Union (removes duplicates, which is equivalent to applying distinct by default), and union all does not remove duplicates.

In general, we use union all, because to connect two result sets, of course, two result sets are required, the union all is more efficient than union, and less steps to remove duplicates.

The following two tables are used, Practice:

Example one:

Example two:

Example three:

Example four:

Example five:

Following these two lines of code after execution:

SELECT * FROM dbo. Student
SELECT * FROM dbo. Student

As can be seen, two SQL statements, respectively, detected three rows of records.

Then we change the SQL:

This is also the query out 3 records, the Union keyword, for us to remove the duplicate three records.

Keep looking, the following:

You can see the union all, the query out all the record lines, no removal of duplicates;

Summarize:

sql--Union query "union"

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.