What does UNION All do in MariaDB?
The UNION ALL operator is used to combine the results of two or more SELECT statements into a single result set. The UNION ALL Operator is different from the UNION Operator where the UNION ALL Operator does not remove duplicate rows from the result set. This means that all rows from each SELECT statement are included in the final result even if duplicate records are present.
let’s Setting Up an Environment for Union All Operator
To understand the UNION ALL Operator easily we need a table on which we will perform various operations and queries so here we will create a table called contains which contains employee_id, first_name, last_name, and department as columns.
CREATE TABLE employees (
employee_id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50)
);
INSERT INTO employees (first_name, last_name, department) VALUES
('John', 'Doe', 'Finance'),
('Jane', 'Smith', 'Marketing'),
('Alice', 'Johnson', 'Engineering'),
('Bob', 'Brown', 'Marketing'),
('Charlie', 'Davis', 'Engineering');
Output:
Explanation: The table has been created.
Union All Operator in MariaDB
In MariaDB, the Union All operator is a useful tool for combining results from multiple SELECT queries into a single result set. Unlike the Union operator, Union All retains all rows, including duplicates, making it ideal for merging large datasets efficiently. Whether we are consolidating information from different tables or simplifying complex queries, mastering the Union All operator can speed up our data manipulation tasks in MariaDB. In this article, we’ll explore everything about the Union All Operator with syntax and their examples too.