What is the difference between Union and union all?

Source: Internet
Author: User
Tags create database

Never know what is the difference between Union and union all in the end, today to do a good research, the results of the online search is the following look, but still not very understanding, the following will personally verify:

Union: The two result sets are combined to set operations, excluding duplicate rows, while the default rules are sorted;

Union all: A set of two result sets, including duplicate rows, is not sorted;

The following is a simple test (because it is a test, so the SQL code is written very simply, there is no strict specification, just to understand the difference between the two)

Strict standard writing should first determine whether the database exists, whether the table exists and so on constraints

The first step is to build the library: View plain Create database test go with test go


Step two, build table: View plain CREATE TABLE Table1 (ID int not NULL, name varchar. NOT NULL) Create table Ta Ble2 (id int not NULL, name varchar NOT NULL)

The third step is to insert the test data:
View plain Insert into Table1 values (1,

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.