Reviewed by Aditya Kumar · Last reviewed 2026-03-24
UNION combines the result sets of two or more SELECT statements and automatically removes duplicate rows, effectively performing an implicit DISTINCT operation. In contrast, UNION ALL combines result…
Red Flag: Using UNION 'to be safe' when sources are known to be disjoint—wastes compute. Pro-Move: 'Our incremental union is always UNION ALL; we deduplicate in a separate step with a defined key.'
This easy-level SQL question appears frequently in data engineering interviews at companies like Presidio, Swiggy. While less common, it tests deeper understanding that distinguishes strong candidates.
Start by clearly defining the core concept being asked about. Interviewers want to see that you understand the fundamentals before diving into implementation details. Structure your answer with a definition, then explain the practical application with a concise example. The expert answer includes a code example that demonstrates the implementation pattern.
UNION combines the result sets of two or more SELECT statements and automatically removes duplicate rows, effectively performing an implicit DISTINCT operation. In contrast, UNION ALL combines result sets without checking for or removing duplicate rows, preserving all rows from the combined inputs.
UNION requires the database engine to perform additional work to identify and eliminate duplicates. This typically involves sorting the combined dataset (O(N log N) complexity) or building a hash table (average O(N) complexity), which consumes significant CPU, memory, and potentially I/O resources, especially for large datasets. UNION ALL, on the other hand, is a simpler concatenation operation (O(N) complexity) that merely appends the rows from one result set to another.
Both operators require that the SELECT statements have the same number of columns, and corresponding columns must have compatible data types.
UNION is generally much slower and more resource-intensive than UNION ALL. In distributed systems like Apache Spark, the UNION operation can trigger a "shuffle" across network partitions to gather and sort data for deduplication, leading to substantial network I/O and latency. For very large datasets, UNION can lead to disk spills if memory is insufficient, further degrading performance.
UNION ALL is the preferred choice for performance and scalability unless the business logic explicitly requires unique rows. It's often used when combining data from different sources that are known to have disjoint sets of primary keys or when duplicates are acceptable/expected.
-- Example: Combining two tables
SELECT id, name FROM employees_current
UNION ALL -- Faster, keeps all rows including duplicates
SELECT id, name FROM employees_archive;
-- If uniqueness is critical:
SELECT id, name FROM employees_current
UNION -- Slower, removes duplicate (id, name) pairs
SELECT id, name FROM employees_archive;
In the interview, also mention that UNION's deduplication can be expensive in a distributed environment, often necessitating a data shuffle, which is a major performance consideration for large-scale data processing.
Red Flag: Using UNION 'to be safe' when sources are known to be disjoint—wastes compute. Pro-Move: 'Our incremental union is always UNION ALL; we deduplicate in a separate step with a defined key.'
Some links below are affiliate links. If you buy through them we may earn a small commission at no extra cost to you — it helps keep DataEngPrep free.
According to DataEngPrep.tech, this is one of the most frequently asked SQL interview questions, reported at 2 companies. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.