Skip to content
/ Chapter 15 · Set Operations
Lesson 15.2 · garden_shop
Lesson 15.2

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.

Pattern
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
Table · suppliers

One row per supplier. Some suppliers have no contact email.

4 columns · 6 rows
supplier_id int supplier_name text contact_email text state text
Table · customers

One row per customer. Some customers have no phone on file.

9 columns · 20 rows
customer_id int first_name text last_name text email text phone text city text state text signup_date date is_active bool
Your task

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.