SQL Basic
SQL Advanced
SQL Functions
| SQL Union |
|
The SQL UNION operator combines two or more SELECT statements. The SQL UNION OperatorThe UNION operator is used to combine the result-set of two or more SELECT statements. Notice that 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. SQL UNION Syntax
Note: The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL. SQL UNION ALL Syntax
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. SQL UNION ExampleLook at the following tables: "Employees_Norway":
"Employees_USA":
Now we want to list all the different employees in Norway and USA. We use the following SELECT statement:
The result-set will look like this:
Note: This command cannot be used to list all employees in Norway and USA. In the example above we have two employees with equal names, and only one of them will be listed. The UNION command selects only distinct values. SQL UNION ALL ExampleNow we want to list all employees in Norway and USA:
Result
|
SQL Union