About Lesson
Unions in MySQL
In MySQL, “UNION’ is used to combine the results of two or more `SELECT` queries into a single result set. The queries must have the same number of columns and compatible data types. The main points to consider when using UNION are:
– UNION’ automatically removes duplicate rows.
– **UNION ALL” includes all duplicate rows in the result.
Syntax:-
SELECT column_list FROM table1
UNION [ALL]
SELECT column_list FROM table2;
Example:-
sql
SELECT id, name FROM customers
UNION
SELECT id, name FROM suppliers;
This query combines the results from the `customers` and `suppliers` tables, removing duplicates. If you want to keep duplicates, use `UNION ALL` instead.