SQL Union and SQL UNION ALL usage

Source: Internet
Author: User

SQL Union Usage


SQL Union and SQL UNION ALL usage

SQL UNION operator

  1. The
  2. UNION operator is used to combine the result set of two or more SELECT statements.

    Notice that the SELECT statement inside the UNION must have the same number of columns. The column must also have a similar data type. Also, the order of the columns in each SELECT statement must be the same.

    SQL UNION Syntax Select column_name (s) from Table_name1unionselect column_name (s) from table_name2

    Comment: By default, The Union operator chooses a different value, that is, the Union is going to be heavy. If duplicate values are allowed, use UNION all.

    SQL UNION All syntax select COLUMN_NAME (s) from Table_name1union Allselect column_name (s) from table_name2

    Additionally, The column name in the union result set is always equal to the column name in the first SELECT statement in the Union. The purpose of the

    union  directive is to combine the results of two SQL statements. From this point of view, union  is somewhat similar to JOIN, as these two instructions can be retrieved from multiple tables. UNION only shows two results together, not the syntax for joining two tables ............ union :

    650) this.width=650; "src="/HTTP/ c.hiphotos.baidu.com/exp/w=500/sign=da65f59da918972ba33a00cad6cc7b9d/ F703738da9773912b9684ce8fa198618377ae2e9.jpg "class=" Exp-image-default "alt=" SQL Union usage "/>

  3. And 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_informationunionselect date from internet_sales result:

    650) this.width=650; "Src=" http://d.hiphotos.baidu.com/exp/w=500/sign=1cc5f39d40a7d933bfa8e4739d4ad194/ Dbb44aed2e738bd4a06a7bb8a38b87d6267ff9f4.jpg "class=" Exp-image-default "alt=" SQL Union usage "/>

  4. One thing to note is that if we use "Select DISTINCT Date" in any SQL statement (or both), we will get exactly the same result.


This article is from the "11100713" blog, please be sure to keep this source http://11110713.blog.51cto.com/11100713/1834136

SQL Union and SQL UNION ALL usage

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.