SQL Union
UNION combines compatible result sets and removes duplicate rows. Use it when two queries describe the same kind of output and duplicates should appear only once.
Understand the idea
Both SELECT statements must return the same number of columns in compatible positions. Output column names normally come from the first SELECT. This example deliberately chooses Nadiad twice through different filters. UNION compares the complete selected row, so selecting an extra identifier would change which rows count as duplicates.
Where to try it
Run this example in the SQL playground. The lesson link loads the shop tables and sample rows before this query. Select Run when you are ready. Every run starts fresh, so your changes do not affect the website or a previous run.
SQLite sample data · MySQL / MariaDB sample data · See the sample tables
A worked example
SELECT city FROM customers WHERE customer_id IN (1, 2)
UNION
SELECT city FROM customers WHERE customer_id = 3
ORDER BY city;
Read the result
The result contains Nadiad and Surat. Nadiad appears only once even though the two branches both supply it.
Try one change
Add name to both SELECT lists. Explain why two customers from Nadiad no longer collapse into one result row.