The UNION ALL operator combines results from multiple SELECT queries and returns all rows, including duplicates. It’s useful when you want raw merged data without filtering unique values.
UNION since it skips the duplicate-removal step.SELECT must return the same number of columns.
// Basic UNION ALL syntax combining results
SELECT column_list FROM table1
UNION ALL
SELECT column_list FROM table2;
// Creating tables and using UNION ALL
CREATE TABLE Sales_Q1 (Product VARCHAR(50), Amount INT);
CREATE TABLE Sales_Q2 (Product VARCHAR(50), Amount INT);
INSERT INTO Sales_Q1 VALUES ('Laptop', 1000), ('Mouse', 200), ('Laptop', 1000);
INSERT INTO Sales_Q2 VALUES ('Laptop', 1500), ('Keyboard', 300);
SELECT Product, Amount FROM Sales_Q1
UNION ALL
SELECT Product, Amount FROM Sales_Q2;
UNION instead if you want only unique rows.ORDER BY only at the end of the entire query.UNION ALL for performance when duplicates don’t matter.ORDER BY after the final SELECT block.UNION ALL.UNION ALL with UNION.ORDER BY Product to sort combined results.