maria-db
  1. maria-db-union-all-operator

Union All Operator - (MariaDB Operators)

The UNION ALL operator in MariaDB is used to combine the results of two or more SELECT statements into a single result set. Unlike the UNION operator, UNION ALL does not remove duplicates from the result set.

Syntax

The syntax for using the UNION ALL operator in MariaDB is as follows:

SELECT column1, column2, ... FROM table1
UNION ALL
SELECT column1, column2, ... FROM table2
UNION ALL
SELECT column1, column2, ... FROM table3
...

Here, we are selecting the columns we want to include in our result set from each table, and then combining the results using the UNION ALL operator.

Example

Here is an example of using the UNION ALL operator to combine the results of two SELECT statements that retrieve data from different tables:

SELECT name, age FROM employees
UNION ALL
SELECT name, age FROM contractors;

Output

Executing the above SQL statement will return a result set that includes all of the rows from both the employees and contractors tables, with duplicates included.

Explanation

In the above SQL code, we are selecting the name and age columns from both the employees and contractors tables, and then combining the results using the UNION ALL operator. The resulting output will contain all of the rows from both tables, regardless of whether they are duplicates or not.

Use

The UNION ALL operator is useful when we want to combine the results of multiple SELECT statements into a single result set, without removing duplicates. This is helpful in situations where we want to include duplicate records in our result set, such as when we are combining results from two or more similar tables.

Important Points

  • The UNION ALL operator is used to combine the results of two or more SELECT statements into a single result set.
  • Unlike the UNION operator, UNION ALL does not remove duplicates from the result set.
  • The UNION ALL operator is helpful when we want to include duplicate records in our result set.

Summary

In summary, the UNION ALL operator in MariaDB is used to combine the results of two or more SELECT statements into a single result set, without removing duplicates. It is useful in situations where we want to include duplicate records in our result set, such as when we are combining results from two or more similar tables.

Published on: