What is the difference between UNION and UNION ALL?

Why Interviewers Ask This

This is a classic screening question for MySQL / SQL roles. Hiring managers ask it early in interviews to gauge your baseline understanding and determine if you can communicate technical concepts clearly.

Answer

UNION combines results from two or more SELECT statements and automatically removes duplicate rows from the combined result — it performs a DISTINCT operation on the entire result set, which requires sorting or hashing to detect duplicates. UNION ALL also combines results but keeps ALL rows including duplicates, with no deduplication step. UNION ALL is always faster than UNION because it skips the expensive deduplication. When to use each: use UNION ALL when you know the queries return different data (different tables, different WHERE conditions that guarantee no overlap), or when duplicates are acceptable; use UNION when you need to eliminate exact duplicate rows. Example: SELECT name FROM employees WHERE department = "Sales" UNION SELECT name FROM employees WHERE department = "Marketing"; — if an employee is in both departments (shouldn't happen but theoretically), UNION removes the duplicate name. UNION ALL would return the name twice. Both require the same number of columns and compatible types. Both support ORDER BY on the final combined result (not within individual SELECT statements in MySQL without subqueries).

Pro Tip

If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.