Lesson 15.1
SQL UNION ALL
UNION ALL stacks compatible result sets and keeps every row from each SELECT.
UNION ALL combines rows from one SELECT with rows from another. Think of it as stacking two result grids with the same column shape.
Each SELECT must return the same number of columns, and the matching columns should have compatible types. Column names come from the first SELECT.
Pattern
select 'source one' as source_name, value_column
from first_table
union all
select 'source two' as source_name, value_column
from second_tableUse UNION ALL when duplicates matter
UNION ALL does not remove duplicates. That makes it faster and more literal than UNION: every row produced by either input is kept in the combined result.
Schema · Garden ShopTable · customers9 columns · 20 rows
Table · customers
9 columns · 20 rowsOne row per customer. Some customers have no phone on file.
customer_id intfirst_name textlast_name textemail textphone textcity textstate textsignup_date dateis_active bool
Your task
Build one Oregon contact list from customers andsuppliers. Return contact_type,contact_name, and state, usingcustomer and supplier as the type labels.
SQL Workbench
⌘↵ to run·
Expected answer
- Columns: contact_type, contact_name, state.
- Rows: Oregon customers plus Oregon suppliers.
- UNION ALL keeps the rows from both inputs without deduplicating.