Junior — Middle
What is the difference between the UNION and UNION ALL operators when combining results in SQL?
sobes.tech AI
Answer from AI
The UNION operator combines the results of two or more SQL queries and automatically removes duplicate rows, returning only unique records. While UNION ALL combines the results without removing duplicates, meaning it returns all rows from all queries, including repetitions.
Example:
SELECT name FROM employees
UNION
SELECT name FROM managers;
This query will return unique names from both tables.
SELECT name FROM employees
UNION ALL
SELECT name FROM managers;
This query will return all names, including duplicates if they exist in both tables.