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

Union Operator - (MariaDB Operators)

The union operator in MariaDB is used to combine the result sets of two or more SELECT statements. The operator returns only distinct rows that appear in either result set.

Syntax

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

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

Here, column1, column2, and so on represent the columns to be retrieved from each table, while table1 and table2 represent the tables to be queried.

Example

Here is an example of using the union operator in MariaDB to combine the result sets of two SELECT statements:

SELECT name, age, nationality FROM employees
UNION
SELECT name, age, nationality FROM customers;

Output

Executing the above query will return a result set that includes distinct rows from the employees and customers tables that have the name, age, and nationality columns.

Explanation

In the above example, we are combining the result sets of two SELECT statements using the union operator. The first SELECT statement retrieves the name, age, and nationality columns from the employees table, while the second SELECT statement retrieves the same columns from the customers table. The union operator returns only distinct rows from the two tables that have all three columns.

Use

The union operator in MariaDB is useful for retrieving data from multiple tables with similar structures. It can be used to combine data from different tables that have similar data types and column names.

Important Points

  • The union operator in MariaDB is used to combine the result sets of two or more SELECT statements.
  • The operator returns only distinct rows that appear in either result set.
  • The columns retrieved from each table must have the same data types and column names.
  • The union operator is useful for retrieving data from multiple tables with similar structures.

Summary

In summary, the union operator in MariaDB is used to combine the result sets of two or more SELECT statements. It returns only distinct rows that appear in either result set and is useful for retrieving data from multiple tables with similar structures. The columns retrieved from each table must have the same data types and column names for the union operator to work properly.

Published on: