Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

To apply ORDER BY, SORT BY, CLUSTER BY, DISTRIBUTE BY or LIMIT to an individual SELECT, place the clause inside the parentheses that enclose the SELECT:

Code Block
SELECT key FROM (SELECT key FROM src ORDER BY key LIMIT 10)a
UNION
SELECT key FROM (SELECT key FROM src1 ORDER BY key LIMIT 10)b

To use an ORDER BY, SORT BY, CLUSTER BY or LIMIT clause to the entire UNION result, parenthesize the individual SELECT statements and place the ORDER BY, SORT BY, CLUSTER BY, DISTRIBUTE BY or LIMIT after the last one. The following example uses both ORDER BY and LIMIT clauses:

Code Block
(SELECT key FROM src)
UNION
(SELECT key FROM src1) src1 
ORDER BY key LIMIT 10

...

Info
titleVersion information

In Hive 0.12.0 and earlier releases, unions can only be used within a subquery such as "SELECT * FROM (select_statement UNION ALL select_statement UNION ALL ...) unionResult".

As of Hive 0.13.0, unions can also be used in a top-level query: "select_statement UNION ALL select_statement UNION ALL ...". (See HIVE-6189.)

Before Hive 1.2.0, only UNION ALL (bag union) is supported, in which duplicates are not eliminated. UNION (or UNION DISTINCT) is supported since Hive 1.2.0. (See HIVE-9039)

...