Examples of union queries

Examples of union queries

Union queries combine fields from two or more tables or queries into one field. Following are examples of a basic union query, and of sorting records, renaming fields, and returning duplicate records in union queries.

This topic provides information about:

Basic union query

Sorting in a union query

Renaming fields in a union query

Returning duplicate records in a union query

Basic union query

The following union query consists of two SQL SELECT statements that return the company names and cities that are in Brazil, from both the Suppliers and Customers tables.

Combine data from two or more tables with a union query

Return to top

Sorting in a union query

The following union query selects all company names and cities from both the Suppliers and Customers tables, and sorts the data alphabetically by city:

SELECT [CompanyName], [City]
FROM [Suppliers]

UNION SELECT [CompanyName], [City]
FROM [Customers]
ORDER BY [City];

Return to top

Renaming fields in a union query

The following union query renames the Company Name field to "Supplier/Customer Name" in the query output:

SELECT [CompanyName] AS [Supplier/Customer Name], [City]
FROM [Suppliers]

UNION SELECT [CompanyName] AS [Supplier/Customer Name], [City]
FROM [Customers];

Return to top

Returning duplicate records in a union query

The following union query uses the UNION ALL statement to retrieve all records, including duplicates:

SELECT [CompanyName], [City]
FROM [Suppliers]

UNION ALL SELECT [CompanyName], [City]
FROM [Customers];

Return to top

More information

Work with SQL in queries, forms, reports, macros, and modules

Ways to bring together data from multiple tables or queries in a query