Union

Combine the results of two (or more) SQL queries.

Syntax
      [TABLE] query1 
         UNION [ALL] [TABLE] query2
           [UNION [ALL] [TABLE] queryn [ … ]]

Key
  query1-n    A SELECT statement, the name of a stored query,
              or the name of a stored table preceded by the TABLE keyword. 

A UNION SQL statement can merge the results of two or more queries, tables, and SELECT statements, in any combination.

The ALL predicate will return all records including any duplicates. This will also make the query run faster. By default duplicates are not returned.

All queries in a UNION operation must request the same number of fields.

Use aliases only in the first SELECT statement - aliases are ignored in all but the first select. In the ORDER BY clause, refer to fields by what they are called in the first SELECT statement.

Examples

Select supplier_id, supplier_name
From suppliers
UNION
Select company_id, company_name
From companies
ORDER BY supplier_name;

“Eventually, all things merge into one, and a river runs through it. The river was cut by the world’s great flood and runs over rocks from the basement of time. On some of the rocks are timeless raindrops. Under the rocks are the words, and some of the words are theirs. I am haunted by waters” ~ Norman Maclean

Related

Avg() - Average
DCount - Count the number of records in a table/query.
Delete - Delete records.
Insert - Add records to a table (append query).
Max() - Return the maximum value from a query.
Select - Retrieve data from one or more tables or queries.
Select Into - Make-table query.
Select-Subquery - SubQuery
Transform - Create a crosstab query.


 
Copyright © 1999-2024 SS64.com
Some rights reserved