Unions

Contact Us or call 1-877-932-8228
Unions

Unions

Unions are used to retrieve records from multiple tables or to get multiple record sets from a single table.

Code Sample:

SubqueriesJoinsUnions/Demos/Unions.sql
/*
Get the phone numbers of all shippers, customers, and suppliers
*/

SELECT CompanyName, Phone
FROM Shippers
	UNION
SELECT CompanyName, Phone
FROM Customers
	UNION
SELECT CompanyName, Phone
FROM Suppliers
ORDER BY CompanyName;

This query will return the company name and phone number of all shippers, customers and suppliers.

UNION ALL

By default, all duplicates are removed in UNIONs. To include duplicates, use UNION ALL in place of UNION.

UNION Rules

  • Each query must return the same number of columns.
  • The columns must be in the same order.
  • Column datatypes must be compatible.
  • In Oracle, you can only ORDER BY columns that have the same name in every SELECT clause in the UNION.
Next