How to use SQL union and Union all

Source: Internet
Author: User
Tags how to use sql

One Limit of Union is that the columns generated by two SQL statements must be of the same data type. In addition, when we use the Union command, we only see different data values (similar to select distinct ). Union only Concatenates the two results and displays them together. It does not join two tables.
The syntax of union is as follows: [SQL statement 1]
Union
[SQL statement 2] Suppose we have the following two tables,
Store_information table store_name sales date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999
Internet sales table date sales
Jan-07-1999 $250
Jan-10-1999 $535
Jan-11-1999 $320
January 12-1999 $750
We need to find out all the days with a turnover. To achieve this goal, we use the following SQL statement:
Select date from store_information
Union
Select date from internet_sales result:
Date
Jan-05-1999
Jan-07-1999
Jan-08-1999
Jan-10-1999
Jan-11-1999
Jan-12-1999
It is worth noting that if we use "select distinct date" in any SQL statement (or both sentences), we will get the same result.
SQL Union all
The purpose of the Union all command is to combine the results of two SQL statements. The difference between Union all and union is that Union all lists each qualified data, regardless of whether the data value is repeated. The syntax of Union all is as follows: [SQL statement 1]
Union all
[SQL statement 2] we use the same example on the previous page to show the differences between Union all and union. Let's assume that we have two tables:
Store_information table store_name sales date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999
Internet sales table date sales
Jan-07-1999 $250
Jan-10-1999 $535
Jan-11-1999 $320
January 12-1999 $750
However, we need to find out the days when there are store turnover and network turnover. To achieve this goal, we use the following SQL statement:
Select date from store_information
Union all
Select date from internet_sales result:
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

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.