TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

HiveQL - ORDER BY and SORT BY Clause

HiveQL - ORDER BY and SORT BY Clause with tutorial, introduction, environment setup, first app hello world, state, props, flexbox, height and width, listview, scrollview, images, buttons, router, etc.

<< Back to HIVEQL

HiveQL - ORDER BY and SORT BY Clause

By using HiveQL ORDER BY and SORT BY clause, we can apply sort on the column. It returns the result set either in ascending or descending order. Here, we are going to execute these clauses on the records of the below table:

HiveQL - ORDER BY and SORT BY Clause

HiveQL - ORDER BY Clause

In HiveQL, ORDER BY clause performs a complete ordering of the query result set. Hence, the complete data is passed through a single reducer. This may take much time in the execution of large datasets. However, we can use LIMIT to minimize the sorting time.

Example of ORDER BY Clause in Hive

Let's see an example to arrange the data in the sorted order by using ORDER BY clause.

  • Select the database in which we want to create a table.
hive> use hiveql;

HiveQL - ORDER BY and SORT BY Clause
  • Now, create a table by using the following command:
hive> create table emp (Id int, Name string , Salary float, Department string)  
row format delimited  
fields terminated by ',' ; 

HiveQL - ORDER BY and SORT BY Clause
  • Load the data into the table.
hive> load data local inpath '/home/codegyani/hive/emp_data' into table emp;

HiveQL - ORDER BY and SORT BY Clause
  • Now, fetch the data in the descending order by using the following command:
hive> select * from emp order by salary desc;

HiveQL - ORDER BY and SORT BY Clause
HiveQL - ORDER BY and SORT BY Clause
HiveQL - ORDER BY and SORT BY Clause

Here, we got the desired result.

HiveQL - SORT BY Clause

The HiveQL SORT BY clause is an alternative of ORDER BY clause. It orders the data within each reducer. Hence, it performs the local ordering, where each reducer's output is sorted separately. It may also give a partially ordered result.

Example of SORT BY Clause in Hive

In this example, we arrange the data in the sorted order by using SORT BY clause.

  • Let's fetch the data in the descending order by using the following command:
hive> select * from emp sort by salary desc;

HiveQL - ORDER BY and SORT BY Clause
HiveQL - ORDER BY and SORT BY Clause
HiveQL - ORDER BY and SORT BY Clause

Here, we got the desired result.

Next TopicHiveQL - JOIN




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf