Skip to content
/Chapter 15 · Set Operations
Lesson 15.1·garden_shop
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_table

Use 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

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

9 columns · 20 rows
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
query.sqlgarden_shop · SQL engine loading
⌘↵ 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.