MySQL UNION Operator

5 (100%) 1 vote

MySQL UNION Operator

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

  • Each SELECT statement within UNION must have the same number of columns
  • The columns must also have similar data types
  • The columns in each SELECT statement must also be in the same order

Example:

Basic Query:

SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2;

Query Example 1:

SELECT City FROM Customers UNION SELECT City FROM Suppliers ORDER BY City;

Query Example 2:

SELECT City, Country FROM Customers
WHERE Country=‘Germany’
UNION
SELECT City, Country FROM Suppliers
WHERE Country=‘Germany’
ORDER BY City;

 

 

For any query or issue, feel free to discuss on http://discuss.eduguru.in
%d bloggers like this: