UNION / UNION ALL
Feb 23, 2026
·
5
min read
Category: Set Operations
Platform Support:
✅ Snowflake | ✅ BigQuery | ✅ Databricks
Description
UNION combines the result sets of two or more SELECT statements into a single result set. UNION removes duplicate rows, while UNION ALL keeps all rows including duplicates. Essential for combining data from multiple sources or tables with similar structures.
Syntax
Requirements:
Same number of columns in each SELECT
Corresponding columns must have compatible data types
Column names from first SELECT are used in result
Platform-Specific Notes
All Platforms:
UNION and UNION ALL work identically
UNION ALL is faster (no deduplication)
ORDER BY applies to entire result (place at end)
Can union more than 2 queries
Example 1: UNION vs UNION ALL
All Platforms:
Sample Data:
premium_customers:
customer_id | name |
|---|---|
1 | John Smith |
2 | Maria Garcia |
standard_customers:
customer_id | name |
|---|---|
2 | Maria Garcia |
3 | Sarah Johnson |
Result with UNION (duplicates removed):
customer_id | name | tier |
|---|---|---|
1 | John Smith | Premium |
2 | Maria Garcia | Premium |
2 | Maria Garcia | Standard |
3 | Sarah Johnson | Standard |
Result with UNION ALL (all rows kept):
customer_id | name | tier |
|---|---|---|
1 | John Smith | Premium |
2 | Maria Garcia | Premium |
2 | Maria Garcia | Standard |
3 | Sarah Johnson | Standard |
Note: In this case both return same results, but UNION ALL is faster
Example 2: Combining Historical and Current Data
All Platforms:
Sample Data:
current_orders:
order_id | customer_id | order_date | total |
|---|---|---|---|
1001 | 101 | 2024-01-15 | 250.00 |
1002 | 102 | 2024-01-20 | 180.00 |
archived_orders:
order_id | customer_id | order_date | total |
|---|---|---|---|
900 | 103 | 2024-01-10 | 320.00 |
901 | 101 | 2024-01-12 | 450.00 |
Result:
order_id | customer_id | order_date | total | data_source |
|---|---|---|---|---|
1002 | 102 | 2024-01-20 | 180.00 | Current |
1001 | 101 | 2024-01-15 | 250.00 | Current |
901 | 101 | 2024-01-12 | 450.00 | Archive |
900 | 103 | 2024-01-10 | 320.00 | Archive |
Example 3: Multi-Source Contact List
All Platforms:
Sample Data:
customers:
name | signup_date | |
|---|---|---|
John Smith | 2024-01-15 | |
Maria Garcia | 2024-01-20 |
newsletter_subscribers:
subscriber_name | subscription_date | |
|---|---|---|
Sarah Johnson | 2024-01-18 | |
John S. | 2024-01-10 |
sales_leads:
contact_email | contact_name | created_date |
|---|---|---|
Ahmed Hassan | 2024-01-22 |
Result (UNION removes exact duplicates):
name | source | source_date | |
|---|---|---|---|
Ahmed Hassan | Lead | 2024-01-22 | |
John S. | Newsletter | 2024-01-10 | |
John Smith | Customer | 2024-01-15 | |
Maria Garcia | Customer | 2024-01-20 | |
Sarah Johnson | Newsletter | 2024-01-18 |
Example 4: Quarterly Sales Aggregation
All Platforms:
Result:
quarter | total_sales | order_count | avg_order_value |
|---|---|---|---|
Q1 2024 | 125000.00 | 450 | 277.78 |
Q2 2024 | 142000.00 | 520 | 273.08 |
Q3 2024 | 138000.00 | 495 | 278.79 |
Example 5: Creating Comprehensive Reports
All Platforms:
Sample Data (sales table):
product_category | product_name | quantity | revenue |
|---|---|---|---|
Electronics | Laptop | 50 | 50000 |
Electronics | Mouse | 200 | 6000 |
Furniture | Desk | 30 | 9000 |
Furniture | Chair | 80 | 16000 |
Result:
product_category | product_name | units_sold | total_revenue |
|---|---|---|---|
Electronics | Laptop | 50 | 50000 |
Electronics | Mouse | 200 | 6000 |
Electronics | CATEGORY TOTAL | 250 | 56000 |
Furniture | Chair | 80 | 16000 |
Furniture | Desk | 30 | 9000 |
Furniture | CATEGORY TOTAL | 110 | 25000 |
GRAND TOTAL | 360 | 81000 |
UNION vs UNION ALL Performance
Aspect | UNION | UNION ALL |
|---|---|---|
Duplicates | Removed | Kept |
Performance | Slower (sorts/deduplicates) | Faster |
Use when | Need unique rows | Know no duplicates OR duplicates acceptable |
Memory | Higher (deduplication) | Lower |
Typical speedup | Baseline | 2-10x faster |
Common Use Cases
Combine partitioned tables: Merge data from monthly/yearly tables
Multi-source aggregation: Combine data from different systems
Historical analysis: Mix current and archived data
Contact consolidation: Merge customer lists from multiple sources
Reporting: Create summary rows with detail rows
A/B testing: Combine test and control groups
UNION with Different Column Names
All Platforms:
Other Set Operations
INTERSECT - Returns only rows that exist in both queries:
EXCEPT/MINUS - Returns rows from first query not in second:
Performance Tips
Prefer UNION ALL: Use unless you specifically need deduplication
Filter before UNION: Apply WHERE clauses to each query
Index appropriately: Index columns used in WHERE clauses
Limit result size: Use LIMIT/TOP after ORDER BY
Consider views: Create view for frequently unioned queries
Avoid over-unioning: Don't union unnecessarily large result sets
Best Practices
Use consistent column order: Makes queries more maintainable
Add source indicators: Include column showing data source
Document business logic: Explain why data is being combined
Test data types: Ensure compatible types across all SELECTs
Use parentheses: Group complex unions for clarity
ORDER BY at end only: Can't order individual SELECTs
Validate results: Check for unexpected duplicates or missing rows





