What is the Difference Between UNION and UNION ALL in SQL?
UNION and UNION ALL are SQL operators used to combine results from multiple SELECT queries.
In simple words:
- UNION combines results and removes duplicates
- UNION ALL combines results and keeps duplicates
Simple Understanding
| Operator | Duplicate Values |
|---|---|
| UNION | Removed |
| UNION ALL | Kept |
Why UNION and UNION ALL are Important
Large applications often store data in:
- Multiple tables
- Multiple databases
- Partitioned systems
UNION operators help combine these results into a single output.
Real-Time Example
Suppose a learning platform stores:
- Java students
- Python students
Java Students Table
| Name |
|---|
| Naresh |
| Rahul |
| Arjun |
Python Students Table
| Name |
|---|
| Rahul |
| David |
| John |
What is UNION?
UNION combines results from multiple SELECT queries and removes duplicate rows.
UNION Syntax
SELECT column_name FROM table1 UNION SELECT column_name FROM table2;
Example
SELECT name FROM java_students UNION SELECT name FROM python_students;
Result
| Name |
|---|
| Naresh |
| Rahul |
| Arjun |
| David |
| John |
Important Observation
Duplicate value:
Rahul
appears only once.
Why?
Because:
UNION automatically removes duplicates.
UNION Internal Flow
First Query Result
|
v
Second Query Result
|
v
Combine Results
|
v
Remove Duplicates
|
v
Final Output
What is UNION ALL?
UNION ALL combines results from multiple SELECT queries and keeps duplicate rows.
UNION ALL Syntax
SELECT column_name FROM table1 UNION ALL SELECT column_name FROM table2;
Example
SELECT name FROM java_students UNION ALL SELECT name FROM python_students;
Result
| Name |
|---|
| Naresh |
| Rahul |
| Arjun |
| Rahul |
| David |
| John |
Important Observation
Duplicate value:
Rahul
appears twice.
Why?
Because:
UNION ALL does not remove duplicates.
UNION ALL Internal Flow
First Query Result
|
v
Second Query Result
|
v
Combine Results Directly
|
v
Final Output
Main Difference Between UNION and UNION ALL
| Feature | UNION | UNION ALL |
|---|---|---|
| Duplicates | Removed | Kept |
| Performance | Slower | Faster |
| Sorting Required | Yes | No |
| Use Case | Unique results | All records |
Why UNION is Slower
UNION performs:
- Duplicate checking
- Sorting or hashing operations
This increases processing time.
Why UNION ALL is Faster
UNION ALL:
- Simply combines results
- Does not check duplicates
This improves performance.
Performance Comparison
UNION | v Combine + Remove Duplicates | v Higher Processing UNION ALL | v Direct Combine | v Faster Processing
Rules for UNION and UNION ALL
Both queries must have:
- Same number of columns
- Compatible data types
Correct Example
SELECT id, name FROM students UNION SELECT id, name FROM teachers;
Invalid Example
SELECT id, name FROM students UNION SELECT id FROM teachers;
Error Reason
Number of columns does not match.
ORDER BY with UNION
ORDER BY should be used:
At the end of final query.
Example
SELECT name FROM java_students UNION SELECT name FROM python_students ORDER BY name;
UNION vs JOIN
| Feature | UNION | JOIN |
|---|---|---|
| Purpose | Combine rows vertically | Combine tables horizontally |
| Result | More rows | More columns |
| Usage | Similar data sets | Related tables |
Visualization
UNION
Table1 Rows
+
Table2 Rows
JOIN
Table1 Columns <-> Table2 Columns
Real-Time Banking Example
Banking systems may combine:
- Savings account transactions
- Current account transactions
Example
SELECT transaction_id, amount FROM savings_transactions UNION ALL SELECT transaction_id, amount FROM current_transactions;
Why UNION ALL?
Because:
- All transaction records are needed
Real-Time E-Commerce Example
E-commerce systems may combine:
- Online orders
- Store orders
Example
SELECT customer_name FROM online_orders UNION SELECT customer_name FROM store_orders;
Why UNION?
Because:
- Duplicate customers should appear only once
Real-Time Analytics Example
Analytics systems heavily use:
UNION ALL
because:
- Performance is critical
- All records are needed
Advantages of UNION
- Removes duplicates automatically
- Produces clean result sets
- Useful for unique reporting
Advantages of UNION ALL
- Faster performance
- No duplicate removal overhead
- Best for large datasets
Disadvantages of UNION
- Slower performance
- Additional sorting overhead
Disadvantages of UNION ALL
- Duplicate data may appear
Best Practices
- Use UNION when unique results are required
- Use UNION ALL for better performance when duplicates are acceptable
- Prefer UNION ALL in analytics and reporting systems
- Ensure column counts and data types match
Related Learning Topics
- Mastering MySQL Joins
- What is GROUP BY in SQL?
- MySQL Aggregate Functions
- What is a Table in SQL?
- MySQL Performance Optimization
Professional Interview Answer
UNION and UNION ALL are SQL operators used to combine results from multiple SELECT queries. UNION removes duplicate rows and returns only unique results, whereas UNION ALL keeps all rows including duplicates. UNION is slower because it performs duplicate checking and sorting operations, while UNION ALL is faster because it directly combines result sets without duplicate removal. UNION is commonly used when unique data is required, whereas UNION ALL is preferred for better performance in large-scale reporting and analytics systems.
Why Interviewers Like This Answer
- Clearly explains duplicate handling
- Includes performance comparison
- Shows query execution understanding
- Provides real-world examples
- Explains enterprise usage scenarios
Frequently Asked Questions
What is difference between UNION and UNION ALL?
UNION removes duplicates, while UNION ALL keeps duplicates.
Which is faster UNION or UNION ALL?
UNION ALL is faster because it does not remove duplicates.
Why UNION is slower?
UNION performs duplicate checking and sorting operations.
Can UNION combine different column counts?
No, both queries must return same number of columns.
When should UNION ALL be used?
UNION ALL should be used when all records including duplicates are required and performance is important.