Essential cookies keep authentication working. With your permission, we also use analytics cookies to understand and improve the product. Read our Privacy Policy

DataEngPrep.tech
QuestionsPracticeAI CoachDashboardPricingBlog
ProLogin
Home/Questions/SQL/Explain the difference between UNION and UNION ALL.

Explain the difference between UNION and UNION ALL.

SQLeasy2 min read

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…

🤖 Analyze Your Answer
Frequency
Low
Asked at 2 companies
Category
487
questions in SQL
Difficulty Split
130E|271M|86H
in this category
Total Bank
1,863
across 7 categories
Asked at these companies
PresidioSwiggy
Interview Pro Tip

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.'

Why This Question Matters

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.

How to Approach This

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.

Expert Answer
334 wordsIncludes code

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.

Mechanics and Performance

The core difference lies in the deduplication step. 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.

Trade-offs and Scalability

Due to the deduplication overhead, 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.

⚡
Pro Tip

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.'

Want all answers as a PDF for offline study?
Seven focused volumes with 750+ in-depth answers — Answer Vault →

Related SQL Questions

mediumWrite an SQL query to find the second-highest salary from an employee table.FreemediumDemonstrate the difference between DENSE_RANK() and RANK()FreemediumDiscuss differences between ROW_NUMBER(), RANK(), and DENSE_RANK(), and provide examples from your projects.FreemediumExplain the differences between Data Warehouse, Data Lake, and Delta LakeFreemediumExplain the differences between Repartition and Coalesce. When would you use each?Free

Level up your prep

Recommended
Educative
Educative Unlimited

800+ hands-on courses — Grokking System Design, Coding Patterns, and AI mock interviews for your DE loop.

Start learning →

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.

← Back to all questionsMore SQL questions →
Categories
All QuestionsSQLSpark / Big DataPython / CodingSystem DesignCloud / ToolsBehavioral
By Company
AmazonGoogleDatabricksSnowflakeAWSAzureMicrosoftNetflixUberTCS
Interview Guides
All GuidesTop SQL QuestionsTop Spark QuestionsPySpark QuestionsTop Python QuestionsTop System DesignKafka QuestionsAirflow QuestionsSQL Window FunctionsETL QuestionsData Modeling
Products
AI Interview CoachAnswer AnalyzerSQL PlaygroundResume AnalyzerAnswer Vault PDFsPricing
Company
About & Editorial PolicyContact UsAI DisclosureDisclaimerTerms of ServicePrivacy Policy
© 2026 DataEngPrep.tech. All rights reserved.
AboutBlogContactDisclaimer