UNION ALL concatenates result sets directly without deduplication or sorting, making it faster and lighter on memory and tempdb. UNION (distinct) removes duplicate rows across the combined result sets, typically requiring a sort or hash-aggregate operator that adds CPU, memory, and tempdb overhead.
- Behavior Difference: Deduplication and NULL Handling Aspect UNION ALL UNION (UNION DISTINCT) Duplicate rows Keeps all rows from every query Removes duplicate rows across combined results Sorting / ordering Does not sort for deduplication; output order is not guaranteed without an outer ORDER BY Often sorts or hashes rows to find duplicates (still requires an outer ORDER BY for guaranteed final order) NULL values Treated as distinct rows (all NULL rows kept) Treated as equal when deduplicating (multiple identical NULL rows collapse into one) Column count & types Must match in count and be implicitly/explicitly convertible across queries Must match in count and be implicitly/explicitly convertible across queries
- Performance Difference: Execution Plan and Resource Cost UNION ALL (Concatenation): SQL Server and other relational engines typically execute a Concatenation operator that streams rows from each branch as they arrive. It avoids full-result sort or hash-table builds unless an outer ORDER BY or aggregation is present.
UNION (Distinct): To guarantee uniqueness across all branches, the optimizer usually inserts a Sort (Stream Aggregate / Distinct Sort) or Hash Match (Aggregate) operator over the combined rowset.
Large rowsets or wide rows can spill sort/hash worktables to tempdb.
Memory grants increase to hold hash tables or sort buffers.
When you already know the sets are disjoint (for example, partitioned date ranges or distinct status buckets) or duplicates are acceptable, UNION ALL avoids paying for deduplication.
- T-SQL Example: Comparing UNION ALL vs. UNION SQL – Fast: keeps all rows without deduplication overhead SELECT order_id, customer_id, order_date FROM dbo.orders_archive WHERE order_date >= ‘2026-01-01’ UNION ALL SELECT order_id, customer_id, order_date FROM dbo.orders_current WHERE order_date >= ‘2026-01-01’;
– Slower when rowsets are large: deduplicates across both sets SELECT order_id, customer_id, order_date FROM dbo.orders_archive WHERE order_date >= ‘2026-01-01’ UNION SELECT order_id, customer_id, order_date FROM dbo.orders_current WHERE order_date >= ‘2026-01-01’; 4. Operational Checklist Default to UNION ALL unless you specifically need duplicate elimination and duplicates are actually possible in your combined result set.
Remember NULL equality during UNION deduplication: two rows with identical values and NULL in the same column count as duplicates under UNION.
Always add an outer ORDER BY if deterministic output order is required—never rely on implicit ordering from a UNION sort operator.
Watch tempdb and memory grants on large UNION queries; inspect the actual execution plan to see if Sort or Hash Match spills to tempdb.
