MySQL UNION Operator: A Complete Guide with Examples
What is the UNION Operator in MySQL?
The UNION
operator in MySQL is used to combine the results of two or more SELECT
statements into a single result set. By default, it removes duplicate rows from the final output.
Key Features of UNION
- Combines results from multiple
SELECT
queries. - Removes duplicates unless
UNION ALL
is used. - Requires the same number of columns in all
SELECT
queries. - Corresponding columns must have compatible data types.
Syntax
- Combines results from multiple
SELECT column1, column2, ...
FROM table1
UNION
SELECT column1, column2, ...
FROM table2;
For keeping duplicates:
SELECT column1, column2, ...
FROM table1
UNION ALL
SELECT column1, column2, ...
FROM table2;
Examples of UNION Operator
1. Combining Results from Two Tables
Retrieve all employee names from two different tables: full_time_employees
and part_time_employees
.
Query:
SELECT name FROM full_time_employees
UNION
SELECT name FROM part_time_employees;
Result:
+----------+
| name |
+----------+
| Alice |
| Bob |
| Charlie |
+----------+
2. Using UNION ALL
Include duplicate names in the result.
Query:
SELECT name FROM full_time_employees
UNION ALL
SELECT name FROM part_time_employees;
3. Combine Data with WHERE Clause
Retrieve employees working in “HR” or “Finance” from two tables.
Query:
SELECT name FROM full_time_employees WHERE department = 'HR'
UNION
SELECT name FROM part_time_employees WHERE department = 'Finance';
4. Combining with ORDER BY
Sort the combined results alphabetically.
Query:
SELECT name FROM full_time_employees
UNION
SELECT name FROM part_time_employees
ORDER BY name;
Best Practices for Using UNION
Ensure Column Count Matches
Ensure Column Count Matches
The number of columns and their order must be the same in all SELECT
statements.
Use Compatible Data Types
Use Compatible Data Types
Corresponding columns in the queries should have compatible types (e.g., VARCHAR
with VARCHAR
or INT
with INT
).
Optimize with UNION ALL
Optimize with UNION ALL
Use UNION ALL
when you do not need to eliminate duplicates, as it is faster.
Combine with LIMIT for Performance
Combine with LIMIT for Performance
Limit the number of rows in each query before applying UNION
.
Example:
SELECT name FROM full_time_employees LIMIT 10
UNION
SELECT name FROM part_time_employees LIMIT 10;
Index Columns Used in Queries
Index Columns Used in Queries
Proper indexing improves performance for large datasets.
MySQL Workbench Instructions
- Open MySQL Workbench and connect to your database.
- Write a
SELECT
query using theUNION
operator in the SQL editor. - Click Execute to view the combined results.
- Use the result grid to analyze and verify the output.
Common Issues and Troubleshooting
Mismatch in Column Count
Ensure allSELECT
statements have the same number of columns.Incompatible Data Types
Verify that corresponding columns have compatible data types to avoid errors.Duplicate Handling
If duplicates persist unexpectedly, check if you accidentally usedUNION ALL
instead ofUNION
.Performance on Large Datasets
Optimize queries using indexes and filters (WHERE
clauses) before applyingUNION
.
MySQL UNION
Quiz-summary
0 of 5 questions completed
Questions:
- 1
- 2
- 3
- 4
- 5
Information
Quiz: Test Your Knowledge
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading...
You must sign in or sign up to start the quiz.
You have to finish following quiz, to start this quiz:
Results
0 of 5 questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 points, (0)
Categories
- Not categorized 0%
- 1
- 2
- 3
- 4
- 5
- Answered
- Review
- Question 1 of 5
1. Question
What does the UNION operator do in MySQL?
CorrectIncorrect - Question 2 of 5
2. Question
Which query removes duplicates from combined results?
CorrectIncorrect - Question 3 of 5
3. Question
What must be true for columns in UNION queries?
CorrectIncorrect - Question 4 of 5
4. Question
What does UNION ALL do?
CorrectIncorrect - Question 5 of 5
5. Question
Which query combines employees from HR and Finance departments without duplicates?
CorrectIncorrect