SQL-Union vs Union All


Difference between the UNION vs UNION ALL

Feature UNION UNION ALL
Purpose Combines the result sets of two or more SELECT statements and removes duplicate rows from the final result. Combines the result sets of two or more SELECT statements and includes all rows, including duplicates, in the final result.
Duplicate Handling Automatically eliminates duplicate rows by comparing all columns in the result sets. Retains duplicate rows; does not perform any duplicate elimination.
Performance Slower than UNION ALL because of the additional step to check and remove duplicates. Faster than UNION because no duplicate checking is performed.
Use Case Used when you need a distinct set of rows in the final result. Used when duplicates are acceptable or desired in the final result.
Sorting Performs an implicit sorting of the combined result set to facilitate duplicate elimination. Does not perform any implicit sorting, making it more efficient for larger datasets.
Error Handling Requires the same number and type of columns in all SELECT statements to avoid syntax errors. Same as UNION; the number and type of columns in all SELECT statements must match.
Memory Usage Consumes more memory as it needs to store intermediate results and sort them to identify duplicates. Consumes less memory since no duplicate checking or sorting is required.
Example Syntax SELECT FirstName, LastName FROM EmployeesA
UNION
SELECT FirstName, LastName FROM EmployeesB;
SELECT FirstName, LastName FROM EmployeesA
UNION ALL
SELECT FirstName, LastName FROM EmployeesB;
Output Example Combines rows from both tables but excludes duplicate rows:
John Doe, Jane Smith, Sam Wilson (if duplicates existed, only one is shown).
Combines rows from both tables, including all duplicates:
John Doe, Jane Smith, Sam Wilson, John Doe (duplicates are retained).
Performance Tip Use only when duplicates need to be removed, as duplicate elimination can be resource-intensive for large datasets. Use for better performance when duplicates are irrelevant or desirable in the output.
Primary Use Case - When you need distinct values from multiple tables or queries. - When combining datasets where duplicates are meaningful, such as log data or full lists of records.
Example Scenario - Retrieving a list of unique employees from two departments. - Combining sales records from multiple branches, where each record must be preserved for further analysis.

Next