SQL UNION ALL Operator - Complete Beginner Tutorial
Quick Answer
The SQL UNION ALL operator combines the results of two or more SELECT statements including duplicates. Unlike UNION, it does not remove duplicate rows, making it faster and useful when duplicates are needed or when performance is critical.
Learning Objectives
- Understand the purpose and behavior of the SQL UNION ALL operator.
- Learn the syntax for using UNION ALL with multiple SELECT statements.
- Identify differences between UNION and UNION ALL and when to use each.
Introduction to SQL UNION ALL
SQL set operators allow you to combine results from multiple SELECT queries into a single result set.
UNION ALL is a set operator that appends all rows from each query, including duplicates, unlike UNION which removes duplicates.
Combine your data, keep every row.
What is UNION ALL?
UNION ALL is used to combine the results of two or more SELECT statements into a single result set.
It includes all rows from each query, even if some rows appear multiple times.
- Includes duplicates in the final result.
- Does not sort or remove duplicate rows.
- Requires each SELECT to have the same number of columns.
- Columns must have compatible data types.
Syntax of UNION ALL
The basic syntax of UNION ALL combines multiple SELECT statements separated by the UNION ALL keyword.
| Syntax Element | Description |
|---|---|
| SELECT column_list FROM table1 | First query to retrieve data |
| UNION ALL | Operator to combine results including duplicates |
| SELECT column_list FROM table2 | Second query to retrieve data |
Example Usage of UNION ALL
Here is a practical example demonstrating how UNION ALL works.
Example: Combining Customer Lists
Suppose you have two tables, Customers_US and Customers_EU, and you want to list all customers including duplicates.
Differences Between UNION and UNION ALL
Understanding the difference helps you choose the right operator for your needs.
- UNION removes duplicate rows, UNION ALL includes duplicates.
- UNION performs sorting and duplicate elimination, which can impact performance.
- UNION ALL is faster because it simply appends results.
- Use UNION when you need distinct results; use UNION ALL when duplicates are acceptable or performance is critical.
Best Practices and Performance Considerations
Using UNION ALL effectively can improve query performance and clarity.
- Use UNION ALL when you do not need to remove duplicates to improve speed.
- Ensure all SELECT statements have matching columns and compatible data types.
- Avoid unnecessary UNION ALL operations on large datasets without indexing.
- Consider indexing columns used in combined queries for better performance.
Practical Example
This query combines customer lists from two tables including all duplicates.
Examples
SELECT CustomerID, CustomerName FROM Customers_US
UNION ALL
SELECT CustomerID, CustomerName FROM Customers_EU;This query combines customer lists from two tables including all duplicates.
Best Practices
- Use UNION ALL when you want to include duplicates for accurate data representation.
- Verify that all SELECT statements have the same number of columns and compatible data types.
- Use UNION ALL to improve performance when duplicate elimination is not required.
- Test queries on sample data to understand the effect of duplicates.
Common Mistakes
- Using UNION ALL when you actually want to remove duplicates.
- Mismatching column counts or incompatible data types across SELECT statements.
- Assuming UNION ALL sorts the results (it does not).
- Ignoring performance implications on large datasets.
Hands-on Exercise
Combine Employee Data Using UNION ALL
Write a SQL query that combines employee names from two tables, Employees_NY and Employees_CA, including duplicates.
Expected output: A combined list of employee names from both tables including duplicates.
Hint: Use UNION ALL to combine the SELECT statements.
Compare UNION and UNION ALL Results
Write two queries: one using UNION and one using UNION ALL on the same tables. Observe and explain the difference in results.
Expected output: Explanation of how UNION removes duplicates and UNION ALL includes them.
Hint: Focus on duplicate rows and result count.
Interview Questions
What is the difference between UNION and UNION ALL in SQL?
InterviewUNION removes duplicate rows from the combined result set, while UNION ALL includes all rows including duplicates. UNION ALL is faster because it does not perform duplicate elimination.
When should you use UNION ALL instead of UNION?
InterviewUse UNION ALL when you want to include duplicates or when performance is critical and duplicate elimination is unnecessary.
What requirements must SELECT statements meet to be combined with UNION ALL?
InterviewAll SELECT statements must have the same number of columns and compatible data types in corresponding positions.
MCQ Quiz
1. What is the best first step when learning UNION ALL?
A. Understand the purpose and basic idea
B. Skip directly to advanced implementation
C. Ignore examples and practice
D. Memorize terms without context
Correct answer: A
Starting with the purpose and basic idea makes later examples and practice easier to understand.
2. Which activity helps reinforce UNION ALL?
A. Reading once without practice
B. Building or writing a small practical example
C. Avoiding review questions
D. Skipping the summary
Correct answer: B
A small practical example helps connect the topic to real usage.
3. Which statement is most accurate about this topic?
A. The SQL UNION ALL operator combines the results of two or more SELECT statements including duplicates.
B. UNION ALL never needs examples
C. UNION ALL is unrelated to practical work
D. UNION ALL should be learned without checking results
Correct answer: A
The correct option is based on the available topic explanation.
Key Takeaways
- UNION ALL combines result sets including duplicates.
- It is faster than UNION because it does not perform duplicate elimination.
- Use UNION ALL when duplicates are acceptable or desired.
- All SELECT statements combined by UNION ALL must have the same number of columns and compatible data types.
- UNION ALL is useful for merging data from similar tables or partitions.
Summary
The SQL UNION ALL operator is a powerful tool to combine results from multiple SELECT queries including duplicates.
It is faster than UNION because it does not remove duplicates or sort results.
Understanding when and how to use UNION ALL helps you write efficient and accurate SQL queries.
Frequently Asked Questions
Does UNION ALL remove duplicate rows?
No, UNION ALL includes all rows from the combined queries, including duplicates.
Can I use UNION ALL with different numbers of columns in SELECT statements?
No, all SELECT statements combined with UNION ALL must have the same number of columns with compatible data types.
Is UNION ALL faster than UNION?
Yes, UNION ALL is generally faster because it does not perform duplicate elimination or sorting.
When should I prefer UNION over UNION ALL?
Use UNION when you need a distinct set of results without duplicates.





