Last Updated: 25 June, 2023
The UNION ALL operator in MySQL is used to combine the result sets of two or more SELECT statements into a single result set. It concatenates the rows from each SELECT statement, including duplicates.
Here's the basic syntax for using UNION ALL
SELECT column1, column2, ... FROM table1 UNION ALL SELECT column1, column2, ... FROM table2;
Here's an example to illustrate the usage of UNION ALL
SELECT name, age FROM employees WHERE department = 'Marketing' UNION ALL SELECT name, age FROM employees WHERE department = 'Sales'
In this example, we are combining the result sets of two SELECT statements. The first SELECT statement retrieves the names and ages of employees in the Marketing department, while the second SELECT statement retrieves the names and ages of employees in the Sales department. The UNION ALL operator merges the results into a single result set.
Remember that UNION ALL returns all rows, including duplicates. If we want to eliminate duplicate rows from the result set, we can use the UNION operator instead.
That's all, guys. I hope this MySQL article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com