beginner
UNION ALL Operator
5 min readLast updated: 2026-07-23
Overview
The UNION ALL operator combines the result sets of two or more SELECT queries without removing duplicate rows.
Learning Objectives
- Combine query outputs using
UNION ALL. - Understand why
UNION ALLis faster thanUNION. - Preserve duplicate observations in stacked logs.
Detailed Concept Explanation
UNION ALL appends rows from multiple queries directly into a single result set without checking for duplicates or performing a sort. This makes UNION ALL significantly faster and more resource-efficient than UNION.
Code Examples
SQL
sql
-- Combine historical and archive logs preserving every record
SELECT log_id, log_time, message FROM current_logs
UNION ALL
SELECT log_id, log_time, message FROM archive_logs;
Best Practices
- Default to UNION ALL: Make
UNION ALLyour default choice when stacking datasets unless duplicate suppression is strictly required.
Summary
UNION ALL stacks query result sets fast without performing duplicate suppression.