Table of Contents |
---|
Order, Sort, Cluster, and Distribute By
This describes the syntax of SELECT clauses ORDER BY, SORT BY, CLUSTER BY, and DISTRIBUTE BY. See Select Syntax for general information.
Syntax of Order By
The ORDER BY syntax in Hive QL is similar to the syntax of ORDER BY in SQL language.
Code Block |
---|
colOrder: ( ASC | DESC ) colNullOrder: (NULLS FIRST | NULLS LAST) -- (Note: Available in Hive 2.1.0 and later) orderBy: ORDER BY colName colOrder? colNullOrder? (',' colName colOrder? colNullOrder?)* query: SELECT expression (',' expression)* FROM src orderBy |
...
Note that columns are specified by name, not by position number. However in Hive 0.11.0 and later, columns can be specified by position if when configured as follows:
- For Hive 0.11.0 through 2.1.x, set hive.groupby.orderby.position.alias to true (the default is false).
- For Hive 2.2.0 and later, hive.orderby.position.alias is true by default.
The default sorting order is ascending (ASC).
In Hive 2.1.0 and later, specifying the null sorting order for each of the columns in the "order by" clause is supported. The default null sorting order for ASC order is NULLS FIRST, while the default null sorting order for DESC order is NULLS LAST.
In Hive 3.0.0 and later, order by without limit in subqueries and views will be removed by the optimizer. To disable it, set hive.remove.orderby.in.subquery to false is set to true (the default is false).
Syntax of Sort By
The SORT BY syntax is similar to the syntax of ORDER BY in SQL language.
...
Hive uses the columns in SORT BY to sort the rows before feeding the rows to a reducer. The sort order will be dependent on the column types. If the column is of numeric type, then the sort order is also in numeric order. If the column is of string type, then the sort order will be lexicographical order.
In Hive 3.0.0 and later, sort by without limit in subqueries and views will be removed by the optimizer. To disable it, set hive.remove.orderby.in.subquery to false.
Difference between Sort By and Order By
...