Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MySQL UNION Operator

MySQL UNION Operator

This tutorial introduces the syntax and examples of the MySQL UNION operator.

Description

The MySQL UNION operator is used to combine the results of more than two SELECT statements into a single result set and remove duplicate rows.

The UNION operator must consist of two or more SELECT statements, and the number of columns and the data types at corresponding positions in each SELECT statement must be the same.

Syntax

MySQL UNION operator syntax:



SELECT column1, column2, ...

FROM table1

WHERE condition1

UNION

SELECT column1, column2, ...

FROM table2

WHERE condition2

[ORDER BY column1, column2, ...];

Parameter Description:

Example

  1. Basic UNION operation:
SELECT city FROM customers

UNION

SELECT city FROM suppliers

ORDER BY city;

The above SQL statement selects the unique values of all cities from the customers table and the suppliers table, and sorts them in ascending order by city name.

  1. UNION with filter conditions:


SELECT product_name FROM products WHERE category = 'Electronics'

UNION

SELECT product_name FROM products WHERE category = 'Clothing'

ORDER BY product_name;

The above SQL statement selects the product names in the Electronics and Clothing categories and sorts them in ascending order by product name.

  1. The number of columns and data types in a UNION operation must be the same:
SELECT first_name, last_name FROM employees

UNION

SELECT department_name, NULL FROM departments

ORDER BY first_name;

The returned results include first_name and last_name from the employees table, and department_name and NULL from the departments table; all results are sorted by the first_name column.

  1. Using UNION ALL does not remove duplicate rows:


SELECT city FROM customers

UNION ALL

SELECT city FROM suppliers

ORDER BY city;

The above SQL statement uses UNION ALL to combine all cities from the customers table and the suppliers table without removing duplicate rows.

The UNION operator removes duplicate rows when combining result sets, while UNION ALL does not remove duplicate rows, so UNION ALL may perform better; but if you really want to remove duplicate rows, you can use UNION.


Demonstration Database

In this tutorial, we will use the RUNOOB sample database.

Below is the data selected from the "Websites" table:



mysql> SELECT * FROM Websites;

+----+--------------+---------------------------+-------+---------+

| id | name         | url                       | alexa | country |

+----+--------------+---------------------------+-------+---------+

| 1  | Google       | https://www.google.cm/    | 1     | USA     |

| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |

| 3  | Runoob Tutorial      | http://www.runoob.com/    | 4689  | CN      |

| 4  | 微博          | http://weibo.com/         | 20    | CN      |

| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |

| 7  | stackoverflow | http://stackoverflow.com/ |   0 | IND     |

+----+---------------+---------------------------+-------+---------+

The following is the data of the "apps" APP:



mysql> SELECT * FROM apps;

+----+------------+-------------------------+---------+

| id | app_name   | url                     | country |

+----+------------+-------------------------+---------+

|  1 | QQ APP     | http://im.qq.com/       | CN      |

|  2 | 微博 APP | http://weibo.com/       | CN      |

|  3 | 淘宝 APP | https://www.taobao.com/ | CN      |

+----+------------+-------------------------+---------+

3 rows in set (0.00 sec)


SQL UNION Example

The following SQL statement selects alldistinctcountry values (only distinct values) from the "Websites" and "apps" tables:

Example

SELECT country FROM Websites

UNION

SELECT country FROM apps

ORDER BY country;

The result of executing the above SQL is as follows:

**Note:** UNION cannot be used to list all countries from both tables. If some websites and apps are from the same country, each country is listed only once. UNION selects only distinct values. Use UNION ALL to select duplicate values!


SQL UNION ALL Example

The following SQL statement uses UNION ALL to selectallcountry values (including duplicate values) from the "Websites" and "apps" tables:

Example

SELECT country FROM Websites

UNION ALL

SELECT country FROM apps

ORDER BY country;

The result of executing the above SQL is as follows:


SQL UNION ALL with WHERE

The following SQL statement uses UNION ALL to selectallData for China (CN) (including duplicate values):

Example

SELECT country, name FROM Websites

WHERE country='CN'

UNION ALL

SELECT country, app_name FROM apps

WHERE country='CN'

ORDER BY country;

The result of executing the above SQL is as follows: