SQL guide-union and Union all

Source: Internet
Author: User
Union

The Union command is used to select information about two tables. Similar to the join command. In any case, when the Union command is used, all selected columns must have the same data type.

Note:Union only selects non-repeated values.

SQL Statement 1            UNION            SQL Statement 2

---------------------------------------------

Employees_norway:

Employee_id E_name
01 Hansen, OLA
02 Svendson, Tove
03 Svendson, Stephen
04 Pettersen, Kari

Employees_usa:

Employee_id E_name
01 Turner, Sally
02 Kent, Clark
03 Svendson, Stephen
04 Scott, Stephen

---------------------------------------------

Example of using the Union command

List all employees with different Norwegian and American names.

SELECT E_Name FROM Employees_Norway            UNION            SELECT E_Name FROM Employees_USA

Result

Name
Hansen, OLA
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Scott, Stephen

Note:This command cannot be used to list all employees in Norway and the United States. In the above example, we have two employees with the same name, but only one is listed. Union only selects non-repeated values.

---------------------------------------------

Union all

The Union all command is equivalent to the Union command. Except Union all, all values are selected.

SQL Statement 1            UNION ALL            SQL Statement 2

---------------------------------------------

Example of using the Union all command

List all employees in Norway and the United States.

SELECT E_Name FROM Employees_Norway            UNION ALL            SELECT E_Name FROM Employees_USA

Result

Name
Hansen, OLA
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Svendson, Stephen
Scott, Stephen

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.