Open In App

MySQL UNION ALL Operator

Last Updated : 23 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The UNION ALL operator in MySQL combines the result sets of multiple SELECT statements by retaining all duplicate rows for improved performance and efficiency. It is particularly useful when complete data inclusion, including duplicates is required.

In this article, We will learn about the MySQL UNION ALL Operator by understanding various examples and so on.

What is UNION ALL?

  • The UNION ALL operator in MySQL is used to combine the result sets of two or more SELECT statements.
  • Unlike the UNION operator, UNION ALL does not remove duplicate rows from the result sets.
  • This makes UNION ALL faster and more efficient than UNION when we do not need to eliminate duplicates.

Syntax

The basic syntax for the UNION ALL operator is as follows:

SELECT column1, column2, ...

FROM table1

UNION ALL

SELECT column1, column2, ...

FROM table2;

Here, each SELECT statement fetches data from different tables or the same table based on certain conditions, and UNION ALL combines these results.

How Does UNION ALL Operator Work in MySQL?

  • The UNION ALL statement combines the records returned by two or more SELECT statements and forms a larger dataset by appending the set of values.
  • It joins all the result set of rows from each and every SELECT query.
  • This is done without applying any specific type of condition on the rows.
  • To filter through the rows and therefore includes all the data which includes repetition of data.

Examples of MySQL UNION ALL Operator

To understand MySQL UNION ALL Operator we need a table on which we will perform various operations and queries. Here we will consider a table called employees as shown below:

65

Example 1: Return Single Field using UNION ALL Operator

Let's Retrieve a combined list of employee names who either work in the HR department or hold the position of Developer, including duplicates.

SELECT name FROM employees WHERE department = 'HR'

UNION ALL

SELECT name FROM employees WHERE position = 'Developer';

Output:

12-(1)

Example 2: UNION ALL Operator with ORDER BY Clause & WHERE Option

Let's Suppose we need to retrieve a combined list of names and positions of employees who either work in the HR department or hold the position of Developer and then sort this list by position.

UNION ALL Query with ORDER BY

SELECT name, position FROM employees WHERE department = 'HR'

UNION ALL

SELECT name, position FROM employees WHERE position = 'Developer'

ORDER BY position;

Output:

23

UNION ALL Operator vs UNION Operator

Feature

UNION ALL

UNION

Purpose

Combines results of SELECT statements and includes all duplicates.

Combines results of SELECT statements and removes duplicate rows.

Duplicates

Retains all duplicate rows.

Removes duplicate rows.

Performance

Faster, as it does not perform duplicate removal.

Slower, as it performs a distinct operation to remove duplicates.

Usage Scenario

Useful when you need to include every row from the combined queries, including duplicates.

Useful when you need to eliminate duplicate rows and only see unique results.

SQL Syntax

SELECT column1, column2 FROM table1 UNION ALL SELECT column1, column2 FROM table2;

SELECT column1, column2 FROM table1 UNION SELECT column1, column2 FROM table2;

Efficiency

More efficient for large datasets with duplicates.

Less efficient due to the overhead of duplicate checking.

Advantages of UNION ALL Operator

1. Performance:

  • The main difference between the two is that, while UNION sorts and compares rows with those in a subsequent statement to remove duplicates, UNION ALL does not do that, it is therefore faster than UNION.

2. Complete Data:

  • Saves all records, including duplicates what is important for processes that utilize full data view and data accuracy, for instance, historical reporting.

3. Simpler Query Logic:

  • Thus, using UNION ALL is preferable when duplicates are permitted or even welcome, so that there is no need to include complex conditions to filter all the duplicate records.

4. Resource Efficiency:

  • In general, we have found that UNION ALL takes less system resources compared to UNION largely because UNION ALL does not require the extra computation towards elimination of redundancies, as is done in UNION.

5. Suitable for Incremental Data:

  • Suitable where the results are accumulated gradually adding the dataset over the time, and all records including the duplicates must be kept to provide the comprehensive analysis.

Conclusion

In summary, the UNION ALL operator is ideal for combining datasets without removing duplicates, offering performance benefits over the UNION operator. It is best used when the inclusion of all records, including duplicates is necessary.


Next Article
Article Tags :

Similar Reads