The SQL UNION operator combines two or more SELECT statements.
The UNION operator is used to combine the result-set of two or more SELECT statements.
Note: each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order.
Note: The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL.
PS: The column names in the result-set of a UNION are always equal to the column names in the first SELECT statement in the UNION.
Look at the following tables:
"Friends_Australia":
"Friends_USA":
Now we want to list all the different Friends in Australia and USA.
We use the following SELECT statement:
The result-set will look like this:
Note: This command cannot be used to list all Friends in Australia and USA. In the example above we have two Friends with equal names, and only one of them will be listed. The UNION command selects only distinct values.
Now we want to list all Friends in Australia and USA:
Result
Your Query was successfully sent!