MySQL Wildcards

MySQL Wildcards

MySQL Aliases

MySQL Aliases

MySQL Data Types

MySQL Data Types

MySQL Interview Questions

MySQL Interview Questions and Answers


MySQL UNION Operator

The MySQL UNION operator is allow you to combine the result-set of two or more SELECT statements together if SELECT statement have the same number of columns with similar data types and it should be in the same order.

Note: The MySQL UNION operator removes duplicate rows between the various SELECT statements.

MySQL UNION OPERATOR Syntax

To combine the result-set of two or more SELECT statements together, use the following syntax:

Syntax

SELECT col1,col2... FROM tablename1
UNION
SELECT col1,col2... FROM tablename2;

Parameters:

tablename: The table name from which you want to perform UNION operator.

UNION: To combine the result-set of two or more SELECT statements.

MySQL UNION OPERATOR example

The following MySQL, returns all the different "firstName", "lastName" and "address" from "tblCustomer" and "tblCustomerLoan" table:

Example

SELECT firstName, lastName, address FROM tblCustomer 
UNION
SELECT firstName, lastName, address FROM tblCustomerLoan;

Note:

In the above example, the UNION operator will return all distinict records from "tblCustomer" and "tblCustomerLoan" table.

You can use MySQL Command Line Client to use UNION operator to combine the result-set of two or more SELECT statements together. It will look like this:

MySQL UNION ALL OPERATOR example

The following MySQL, returns all the "firstName", "lastName" and "address" from "tblCustomer" and "tblCustomerLoan" table:

Example

SELECT firstName, lastName, address FROM tblCustomer 
UNION ALL
SELECT firstName, lastName, address FROM tblCustomerLoan;

Note:

In the above example, the UNION ALL operator will return all records from "tblCustomer" and "tblCustomerLoan" table.

You can use MySQL Command Line Client to use UNION ALL operator to combine the result-set of two or more SELECT statements together. It will look like this: