Interview Question

Difference between UNION and UNION ALL?

UNION removes duplicates; UNION ALL preserves every row.

💡 Concept ✅ Quick Revision 🗃️ SQL

Answer

UNION removes duplicate result rows, while UNION ALL keeps them. • Both require compatible result shapes. • UNION needs duplicate-elimination work. • Use UNION ALL when duplicates are valid or the inputs are already disjoint.

💡 SQL Example

SELECT city FROM offices UNION ALL SELECT city FROM warehouses;

Result

All rows, including repeated cities

⚡ Quick Revision

UNION removes duplicates; UNION ALL preserves every row.