Show TOC

Syntax documentationUNION Locate this document in the navigation structure

The keyword UNION allows constructing the union of multiple query specifications. Duplicate rows are implicitly eliminated from the result set unless UNION ALL is specified. In addition, you can order the result of a union using the ORDER BY clause.

Syntax Syntax

  1. <query expression> ::= <query term> 
                                      | <query expression> UNION ( ALL )? <query term>.
    
    <query term>::= <query specification> 
                           | '(' <query expression> ')'.
    
End of the code.
Examples

Example Example

  1. SELECT employee_name FROM employees 
    UNION 
    SELECT name FROM more_employees
    
End of the code.

UNION. This SELECT statement determines the union of all the names of all employees from the tables employees and more_employees. The result set will not contain duplicate names.

Example Example

  1. SELECT empid AS employee_id, sal AS salary FROM employees
    UNION
    SELECT empid AS employee_id, sal AS salary FROM more_employees
    ORDER BY employee_id
    
End of the code.

UNION with an ORDER BY Clause. The result of the query is ordered by employee ID in ascending order. Note that the column name from the result table (employee_id) is used in the ORDER BY clause.

More Information

Query Specification