Introduction
Combining result sets helps you see data from different tables or queries together in one list. It makes comparing and analyzing data easier.
SELECT column1, column2 FROM table1 UNION SELECT column1, column2 FROM table2;
SELECT name FROM customers UNION SELECT name FROM suppliers;
SELECT product FROM store1 UNION ALL SELECT product FROM store2;
CREATE TABLE customers (name VARCHAR(20)); CREATE TABLE suppliers (name VARCHAR(20)); INSERT INTO customers VALUES ('Alice'), ('Bob'); INSERT INTO suppliers VALUES ('Bob'), ('Charlie'); SELECT name FROM customers UNION SELECT name FROM suppliers;
UNION or UNION ALL useful in MySQL?UNION keyword combines results and removes duplicates.SELECT ... UNION SELECT .... UNION ALL keeps duplicates, JOIN and AND are incorrect here.table1 has rows: (1, 'Apple'), (2, 'Banana')table2 has rows: (2, 'Banana'), (3, 'Cherry')SELECT id, name FROM table1 UNION SELECT id, name FROM table2;
SELECT * FROM table1 UNION SELECT * FROM table2;
sales_2023 and sales_2024. Both have columns product_id and units_sold. You want a report showing all sales from both years including duplicates, but also want to sort by units_sold descending. Which query correctly achieves this?ORDER BY 2 DESC sorts by the second column units_sold descending correctly.Pass the test and this pattern moves unknown