What is the difference between UNION and UNION ALL?

SQL
Easy
137.7K views

This question tests knowledge of set operations and performance implications. It evaluates optimization awareness.

Why Interviewers Ask This

Interviewers check if you understand the cost of removing duplicates. UNION_ALL is often faster. Knowing when to use which demonstrates practical optimization skills.

How to Answer This Question

State that UNION removes duplicates and sorts, adding overhead. UNION ALL keeps duplicates and simply appends, making it faster. Suggest using UNION ALL unless distinctness is strictly required.

Key Points to Cover

  • UNION removes duplicates
  • UNION ALL keeps duplicates
  • UNION is slower due to sorting
  • Performance trade-off

Sample Answer

UNION combines results and removes duplicates, performing a distinct operation which adds sorting work and slows execution. UNION ALL keeps all duplicates and usually runs faster because it simply appends result sets. I…

Common Mistakes to Avoid

  • Assuming UNION is always safer
  • Not mentioning performance impact
  • Confusing with INTERSECT

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.

Try it free

Related Interview Questions

Browse all 101 SQL questions