SQL UNION also merges the contents of the same field in multiple tables into a single table

Source: Internet
Author: User

The purpose of the Union directive is to combine the results of two SQL statements. From this point of view, we have this feeling that union and join seem to be similar, because these two instructions can be retrieved from multiple tables. One limitation of the Union is that the columns generated by the two SQL statements need to be of the same type of data. Also, when we use the Union command, we will only see different data values (like SELECT DISTINCT). The union simply joins the two results together and does not join two tables.

     Suppose we have the following two tables:  
store_information table: store_name      sales     &N BSP;DATE&NBSP
                      Los Angeles   &NBSP ; $1500      JAN-05-1999&NBSP
                    &N Bsp San Diego       $ $       JAN-07-1999&NBSP
                      Los Angeles     $       JAN-08-1999&NBSP
  &NB Sp                   Boston          $700       JAN-08-1999 


Internet Sales table: Date Sales
jan-07-1999
jan-10-1999 $535
jan-11-1999 $320
jan-12-1999 $750

If we want to find out all the days that have turnover (sales). To achieve this, we use the following SQL statement:
SELECT Date from Store_information
UNION
SELECT Date from Internet_sales

Results:
Date
jan-05-1999
jan-07-1999
jan-08-1999
jan-10-1999
jan-11-1999
jan-12-1999

The Union will filter out duplicate records after the table link is made, so the resulting set of results will be sorted after the table is connected, the duplicate records are deleted and the results returned.

The purpose of the UNION all directive is to merge the results of the two SQL statements together. The Union all and union differ in that union all lists each qualifying material, regardless of whether the data value is duplicated or not.

If we only use union ALL in the above example, the result will be:

Date
jan-05-1999
jan-07-1999
jan-08-1999
jan-08-1999
jan-07-1999
jan-10-1999
jan-11-1999
jan-12-1999

UNION all simply merges two results and returns. Thus, if there are duplicate data in the two result sets returned, the returned result set will contain duplicate data.

In terms of efficiency, SQL UNION ALL performs much more efficiently than SQL Union because it is important to use SQL Union to perform a row, and the SQL union all does not require a row weight, because for some queries that simply use a table to improve efficiency , you can use SQL union all entirely.

SQL UNION also merges the contents of the same field in multiple tables into a single table

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.