SQL union all syntax

Source: Internet
Author: User

SQL union all syntax
Table Name: table
Field: id username ytime
The result to be implemented is:
Query 1:
Select top 5 * from table where id> 20 order by id asc
Query 2:
Select top 5 * from table where id <20 order by id desc
Then, the sum of query 1 and query 2 is sorted in reverse order by id. The original thought: Query 2 is easy to handle, it is originally in reverse order, and all are less than 20, so this does not need to be changed. Now you need to sort query 1 again.
So I wrote an SQL statement:
Select * from (select top 5 * from table where id> 20 order by id asc) order by id desc
Union all
Select top 5 * from table where id> 20 order by id asc
The running result is: the five records with id> 20 are not in reverse order.

It must be replaced by the following code:
Select * from (select top 5 * from table where id> 20 order by id asc
Union all
Select top 5 * from table where id> 20 order by id asc) order by id desc

Detailed description

Union
The union command is used to select related information from two tables, similar to the join command. However, when the union command is used, the Data Types of all selected columns should be the same.

NOTE: If union is used, only different values are selected.

SQL statement 1 union SQL statement 2
The original table used in the following example:
Employees_china:
E_id e_name
01 zhang, hua
02 wang, wei
03 carter, thomas
04 yang, ming

Employees_usa:
E_id e_name
01 adams, john
02 bush, george
03 carter, thomas
04 gates, bill

Use the union command
Instance
List all different employee names in China and the United States:

Select e_name from employees_china union select e_name from employees_usa
Result
E_name
Zhang, hua
Wang, wei
Carter, thomas
Yang, ming
Adams, john
Bush, george
Gates, bill

Note: This command cannot list all employees in China and the United States. In the above example, we have two employees with the same name. Only one of them is listed. The union command only selects different values.

Union all
The union all command is almost equivalent to the union command, but the union all command lists all values.

SQL statement 1 union all SQL statement 2
Use the union all command
Instance:
List all employees in China and the United States:

Select e_name from employees_china union all select e_name from employees_usa
Result
E_name
Zhang, hua
Wang, wei
Carter, thomas
Yang, ming
Adams, john
Bush, george
Carter, thomas
Gates, bill

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.