What is the difference between UNION and UNION ALL in SQL?
This question evaluates knowledge of set operations and their performance implications.
Why Interviewers Ask This
Choosing the right set operation affects performance and result accuracy. Interviewers check if you understand when to remove duplicates and when to keep all rows for speed.
How to Answer This Question
Explain that UNION removes duplicates (like DISTINCT) while UNION ALL keeps them. Note that UNION is slower due to sorting/hash work. Suggest using UNION ALL when duplicates are expected or irrelevant.
Key Points to Cover
- UNION removes duplicates
- UNION ALL keeps duplicates
- Performance trade-offs
- Sorting overhead
Sample Answer
UNION combines results from two SELECT statements and removes duplicates by performing a distinct operation across all columns, which adds sorting or hash work. UNION ALL simply appends result sets without removing dupli…
Common Mistakes to Avoid
- Assuming UNION is always better
- Ignoring performance cost
- Not checking column compatibility
Sound confident on this question in 5 minutes
Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.