Introduction
UNION lets you combine rows from two or more tables into one list without duplicates. It helps you see all results together.
Jump into concepts and practice - no test required
SELECT column1, column2 FROM table1 UNION SELECT column1, column2 FROM table2;
SELECT name FROM customers_north UNION SELECT name FROM customers_south;
SELECT product_id, product_name FROM products_2022 UNION SELECT product_id, product_name FROM products_2023;
SELECT email FROM newsletter_list1 UNION ALL SELECT email FROM newsletter_list2;
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'), ('Fig'); SELECT name FROM fruits1 UNION SELECT name FROM fruits2;
UNION operator do when combining results from two SELECT queries?Table A: id
1
2
3Table B: id
2
3
4SELECT id FROM A UNION SELECT id FROM B;SELECT name FROM employees UNION SELECT name, department FROM managers;Sales2023(product, amount)
Apple, 100
Banana, 150Sales2024(product, amount)
Banana, 200
Cherry, 300