SQL UNION Operator :\u00a0SQL UNION Operator is used to combine the result-set of two or more SELECT statements.<\/p>\n
SELECT\u00a0column_name(s)<\/em>\u00a0FROM\u00a0table1<\/em><\/strong>
\n UNION<\/strong>
\n SELECT\u00a0column_name(s)<\/em>\u00a0FROM\u00a0table2<\/em>;<\/strong><\/p>\n
The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL:<\/p>\n
SELECT\u00a0column_name(s)<\/em>\u00a0FROM\u00a0table1<\/em><\/strong>
\n UNION\u00a0ALL<\/strong>
\n SELECT\u00a0column_name(s)<\/em>\u00a0FROM\u00a0table2<\/em>;<\/strong><\/p>\n
Note:<\/strong><\/p>\n
The column names in the result-set are usually equal to the column names in the first SELECT statement in the UNION.<\/p>\n
In this tutorial we will use the well-known Northwind sample database.<\/p>\n
Below is a selection from the “Customers” table:<\/p>\n\n\n\n\n\n\n
CustomerID<\/strong><\/td>\n | CustomerName<\/strong><\/td>\n | ContactName<\/strong><\/td>\n | Address<\/strong><\/td>\n | City<\/strong><\/td>\n | PostalCode<\/strong><\/td>\n | Country<\/strong><\/td>\n<\/tr>\n | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1<\/td>\n | Alfreds Futterkiste<\/td>\n | Maria Anders<\/td>\n | Obere Str. 57<\/td>\n | Berlin<\/td>\n | 12209<\/td>\n | Germany<\/td>\n<\/tr>\n | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
2<\/td>\n | Ana Trujillo Emparedados y helados<\/td>\n | Ana Trujillo<\/td>\n | Avda. de la Constituci\u00f3n 2222<\/td>\n | M\u00e9xico D.F.<\/td>\n | 05021<\/td>\n | Mexico<\/td>\n<\/tr>\n | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
3<\/td>\n | Antonio Moreno Taquer\u00eda<\/td>\n | Antonio Moreno<\/td>\n | Mataderos 2312<\/td>\n | M\u00e9xico D.F.<\/td>\n | 05023<\/td>\n | Mexico<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n And a selection from the “Suppliers” table:<\/p>\n\n\n\n\n\n\n
|