Introduction
UNION and UNION ALL combine results from two or more queries into one list. They help you see data from different places together.
SELECT column1, column2 FROM table1 UNION [ALL] SELECT column1, column2 FROM table2;
SELECT name FROM customers_north UNION SELECT name FROM customers_south;
SELECT product FROM store_a UNION ALL SELECT product FROM store_b;
SELECT city FROM offices_europe UNION SELECT city FROM offices_asia;
CREATE TABLE fruits1 (name VARCHAR(20)); CREATE TABLE fruits2 (name VARCHAR(20)); INSERT INTO fruits1 VALUES ('Apple'), ('Banana'), ('Cherry'); INSERT INTO fruits2 VALUES ('Banana'), ('Date'), ('Apple'); SELECT name FROM fruits1 UNION SELECT name FROM fruits2;
UNION and UNION ALL in MySQL?UNION behaviorUNION combines results from two queries and removes duplicate rows to return only unique rows.UNION ALL behaviorUNION ALL combines results but keeps all rows, including duplicates, without filtering.UNION removes duplicate rows, UNION ALL keeps all rows including duplicates. -> Option DUNION ALL in MySQL?UNION ALL with a space between UNION and ALL.SELECT ... UNION ALL SELECT ...; without commas or semicolons between the two SELECTs.Table A:Table B:SELECT id, name FROM A UNION SELECT id, name FROM B;
UNION removes duplicates, so (2, Bob) appears only once in the result.SELECT id FROM table1
UNION ALL
SELECT id FROM table2 ORDER BY id;
Employees:Contractors:UNION ALL.Pass the test and this pattern moves unknown