>
Part | Description |
query1n | A SELECT statement, the name of a stored query, or the name of a stored table preceded by the TABLE keyword. |
TABLE [New Accounts] UNION ALL SELECT * FROM Customers WHERE OrderAmount > 1000;By default, no duplicate records are returned when you use a UNION operation; however, you can include the ALL predicate to ensure that all records are returned. This also makes the query run faster. All queries in a UNION operation must request the same number of fields; however, the fields don't have to be of the same size or data type. Use aliases only in the first SELECT clause because they are ignored in any others. In the ORDER BY clause, refer to fields by what they are called in the first SELECT clause. Notes
SELECT CompanyName, City FROM Suppliers WHERE Country = "Brazil" UNION SELECT CompanyName, City FROM Customers WHERE Country = "Brazil";This example retrieves the names and cities of all suppliers and customers located in Brazil, using constants, an alias, and ordering by an alias.
SELECT CompanyName, City, "Supplier" AS Source FROM Suppliers WHERE Country = "Brazil" UNION SELECT CompanyName, City, "Customer" FROM Customers WHERE Country = "Brazil" ORDER BY City, Source;This example retrieves the names and cities of all suppliers and customers in Brazil and the last names and cities of all employees in South America.
SELECT CompanyName, City FROM Suppliers WHERE Country = "Brazil" UNION SELECT CompanyName, City FROM Customers WHERE Country = "Brazil" UNION SELECT LastName, City FROM Employees WHERE Region = "South America";This example retrieves the names and IDs of all suppliers and customers. This union assumes that there are the same number of columns in each table.
TABLE Customers UNION TABLE Suppliers;