In Apache Hive, like SQL, you can decide to order or sort your data differently based on ordering and distribution requirement. In this post we will look at how SORT BY,  ORDER BY, DISTRIBUTE BY and CLUSTER BY behaves differently in Hive.

Sort By vs Order By vs Group By vs Cluster By in Hive
Sort By vs Order By vs Group By vs Cluster By in Hive


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.

Ordering : It orders data at each of ‘N’ reducers , but each reducer can have overlapping ranges of data.

Outcome : N or more sorted files with overlapping ranges.

Let’s understand with an example of below query:-

Lets assume the number of reducers were set to 2 and output of each reducer is as follows –

Reducer 1 :

Reducer 2 :

As, we can see, values in each reducer output is ordered but total ordering is missing , since we end up with multiple outputs per reducer.


This is similar to ORDER BY in SQL Language.

In Hive, ORDER BY guarantees total ordering of data, but for that it has to be passed on to a single reducer, which is normally performance intensive and therefore in strict mode, hive makes it compulsory to use LIMIT with ORDER BY so that reducer doesn’t get overburdened.

Ordering : Total Ordered data.

Outcome : Single output i.e. fully ordered.

For example :

Reducer :

Hive uses the columns in Distribute By to distribute the rows among reducers. All rows with the same Distribute By columns will go to the same reducer.


It ensures each of N reducers gets non-overlapping ranges of column, but doesn’t sort the output of each reducer. You end up with N or more unsorted files with non-overlapping ranges.

Example ( taken directly from Hive wiki ):-

We are Distributing By x on the following 5 rows to 2 reducer:

Reducer 1

Reducer 2

Note that all rows with the same key x1 is guaranteed to be distributed to the same reducer (reducer 1 in this case), but they are not guaranteed to be clustered in adjacent positions.


Cluster By is a short-cut for both Distribute By and Sort By.

CLUSTER BY x ensures each of N reducers gets non-overlapping ranges, then sorts by those ranges at the reducers.

Ordering : Global ordering between multiple reducers.

Outcome : N or more sorted files with non-overlapping ranges.

For the same example as above , if we use Cluster By x, the two reducers will further sort rows on x:

Reducer 1 :

Reducer 2 :

Instead of specifying Cluster By, the user can specify Distribute By and Sort By, so the partition columns and sort columns can be different.

References : –



You may also be interested in some other BigData posts –

  • Pingback: Hive Strict Mode - Saurz{Code}()

  • kratibadjatya

    Nice explanation..

  • David

    good article! 🙂

    btw can I ask something?

    what is the relation between these quries?

    1. create table x clustered by id sorted by id, time
    >> when does clustwred by sorted works.
    is it ok if not clustered sorted inserted because this will cluster and sort at query time, but this will take more time?

    2. insert into x select * from y cluster by id
    sort by id, time
    >> if I inserted clusterd and sorted data, The query 1 time will be shorten ?

    3. select * from x cluster by id
    sort by id , time

    really confusing. .

  • Pingback: Hive Interview Questions – My Learnings()