Syntax
SELECT columns FROM table_a UNION SELECT columns FROM table_b
Description
The UNION statement combines two different SQL queries into a single result set. UNION is most commonly used with the SELECT statement and joins & left joins to combine the required data from several different tables.
Note: UNION will remove duplicate rows from the result.
Examples
Imagine we have 2 tables table_a and table_b.
table_a table_b ------------------ ---------------- |item |name | |item |name | ------------------ ---------------- |1 |widget | |4 |pen | ------------------ ---------------- |2 |frob | |5 |pencil| ------------------ ---------------- |3 |gizmo | ------------------
Now we will perform the union.
SELECT * FROM table_a UNION SELECT * FROM table_b; ------------------ |item |name | ------------------ |1 |widget | ------------------ |2 |frob | ------------------ |3 |gizmo | ------------------ |4 |pen | ------------------ |5 |pencil | ------------------