SQL UNION
UNION stacks result sets like UNION ALL, then removes duplicate rows from the final output.
UNION combines two result sets and removes duplicate rows. It
is useful when you want one distinct list from multiple sources, such as
every state represented by either customers or suppliers.
The duplicate check compares the full selected row. If you add extra columns such as a source label, rows that used to match may become distinct.
select value_column
from first_table
union
select value_column
from second_table
order by value_column UNION is distinct by default
Reach for UNION when duplicates would be noise. Use
UNION ALL when duplicates are meaningful or when you are
building a row-level audit list.
Schema · Garden Shop Tables · suppliers, customers 2 tables
One row per supplier. Some suppliers have no contact email.
One row per customer. Some customers have no phone on file.
Return one distinct list of state values found in either
customers or suppliers, sorted
alphabetically.
Run your query to inspect the result.
Expected answer
- Columns: state.
- Rows: each state that appears in customers or suppliers, once.
- UNION removes duplicate rows from the combined result.
Related
Comparisons, AND/OR, IN, BETWEEN, and LIKE.
SELECT, WHERE, ORDER BY, and LIMIT at a glance.
Practice ranking, running totals, and top row per group.
Debug filters that match nothing.
Filter window results in a CTE or with QUALIFY.
Nested SELECTs, IN, EXISTS, and correlation.