Set Operations: UNION & UNION ALL in MySQL
Overview & Learning Objectives
While JOINs combine tables horizontally by adding columns, Set Operations combine result sets vertically by stacking rows. In MySQL, `UNION` merges the results of two or more queries into a single output while executing a deduplication pass. In contrast, `UNION ALL` appends rows directly without sorting, running vastly faster and preserving duplicates. Analysts also learn how to emulate `INTERSECT` and `EXCEPT` in MySQL.
- Stack query results vertically using `UNION` and `UNION ALL`.
- Understand the performance advantage of `UNION ALL` over `UNION`.
- Emulate ANSI `INTERSECT` and `EXCEPT` operations in MySQL using `INNER JOIN` and `LEFT JOIN`.
Industry Standard
Standard: Data warehouse engineers at Uber and Meta mandate `UNION ALL` for merging partitioned monthly tables because data is pre-partitioned and deduplication sorting is redundant.
Real-World Example: ScoopCraft Multi-Channel Marketing Contact List
ScoopCraft marketing consolidates customer mobile numbers from app signups and POS walk-in receipts for an SMS campaign.
| Operation | Left Rows | Right Rows | Output Count | Execution Speed |
|---|---|---|---|---|
| UNION ALL | 50,000 app users | 30,000 store users | 80,000 total rows | 0.04 seconds (Fast stream append) |
| UNION | 50,000 app users | 30,000 store users | 62,000 unique rows | 0.45 seconds (Sort deduplication pass) |
Functions & Syntax Covered in this Chapter
Here is the definition, execution syntax, parameter breakdown, and real-world business example for each function/syntax covered in this chapter:
UNION (Distinct Set Combine)
Definition: Combines the result sets of two or more SELECT queries into a single result set, performing an automatic deduplication sort pass across all rows.
SELECT phone FROM retail_customers UNION SELECT phone FROM online_customers;
SELECT queries: Must have identical number of columns with compatible data types in the same order.
SELECT phone FROM store_cust UNION SELECT phone FROM app_custUNION ALL (Fast Set Combine)
Definition: Combines multiple SELECT queries preserving all duplicate rows without performing costly deduplication sorting.
SELECT order_id, amount FROM jan_sales UNION ALL SELECT order_id, amount FROM feb_sales;
SELECT queries: Must match column count and compatible types.
SELECT * FROM q1_orders UNION ALL SELECT * FROM q2_ordersCommon Mistakes & Limitations
- Using `UNION` when `UNION ALL` was intended, accidentally losing valid duplicate business transactions.
- Attempting to use `INTERSECT` or `EXCEPT` in older MySQL versions (MySQL 8.0.31+ added INTERSECT/EXCEPT, but older versions require JOIN emulation).
Keyboard Shortcuts (MySQL Workbench)
| Action | Windows | Mac |
|---|---|---|
| Execute Query | Ctrl + Enter | Cmd + Enter |
Pro Tips & Modern Best Practices
- In a `UNION` query, column names in the final result set are determined by the first `SELECT` statement.
- Only a single `ORDER BY` clause is permitted at the very end of the entire combined union query.