SQL Quiz

SQL UNION Operator

The SQL UNION operator combines the result of two or more
SELECT statements.

The SQL UNION Operator

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

Notice that each SELECT statement within the UNION must have the same number
of columns. The columns must also have similar data types. Also, the columns in
each SELECT statement must be in the same order.

Example

Note: UNION cannot be used to list ALL cities from the two tables. If
several customers and suppliers share the same city, each city will only be
listed once. UNION selects only distinct values. Use UNION ALL to also select
duplicate values!

SQL UNION ALL Example

The following SQL statement uses UNION ALL to select all
(duplicate values also) cities from the "Customers" and "Suppliers" tables:

Example

SELECT City FROM CustomersUNION ALLSELECT City FROM Suppliers
ORDER BY City;

Web Certificates

W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content.
While using this site, you agree to have read and accepted our terms of use,
cookie and privacy policy.
Copyright 1999-2015 by Refsnes Data. All Rights Reserved.