MySQL UNION

Last Updated: March 14, 2022

MySQL UNION operator is used to combine two SELECT queries

  • Coresponding columns of two SELECT statament should have same data types
  • Every SELECT statement should have the same number of columns
  • UNION will select only distinct values
  • UNION ALL will select all values

MySQL UNION Syntax

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

Example

You have two tables named customer and supplier and see the following UNION query

union tables
SELECT customer.name, customer.country FROM customer
UNION
SELECT supplier.name, supplier.country FROM supplier;
union results
Test