Part 77: Combining Results with UNION & UNION ALL
UNION and UNION ALL combine rows from two or more queries into a single result set.
1. UNION vs UNION ALL
UNION: Combines rows and removes duplicates (performs an expensive sort/unique operation).UNION ALL: Combines rows and keeps all duplicates (fast memory append).
Performance Rule: Always use
UNION ALLunless you explicitly require deduplication!
SELECT customer_email AS email FROM customers
UNION ALL
SELECT supplier_email AS email FROM suppliers;
Key Takeaways
- Both queries must have matching column counts and compatible data types.
- Always default to
UNION ALLfor maximum performance.
In Part 78, we explore Set Difference with EXCEPT / MINUS!

