Union
A union operation uses the UNION operator to combine two queries into a single compound query. You can use the UNION operator between two or more SELECT statements to produce a temporary table that contains rows that exist in any or all of the original tables. You can also use the UNION operator in the definition of a view.
- in the Projection clause of the SELECT statement
- in the WHERE clause of the SELECT, INSERT, DELETE, or UPDATE statement.
SELECT * FROM (SELECT col1 FROM tab1 WHERE col1 = 100) AS vtab1(c1),
(SELECT col1 FROM tab2 WHERE col1 = 10
UNION ALL
SELECT col1 FROM tab1 WHERE col1 < 50 ) AS vtab2(vc1);
HCL OneDB™ does not support ordering on ROW types. Because a UNION operation requires a sort to remove duplicate values, you cannot use a UNION operator when either query in the union operation includes ROW type data. However, the database server does support UNION ALL with ROW type data, because this type of operation does not require a sort.
The UNION keyword selects all rows from the two queries, removes duplicates, and returns what is left. Because the results of the queries are combined into a single result, the projection list in each query must have the same number of columns. Also, the corresponding columns that are selected from each table must contain compatible data types (CHARACTER data type columns must be the same length), and these corresponding columns must all allow or all disallow NULL values.
For the complete syntax of the SELECT statement and the UNION operator, see the HCL OneDB Guide to SQL: Syntax. For information specific to the product and any limitations that involve the INTO clause and compound queries, see the HCL OneDB ESQL/C Programmer's Manual.