TheDeveloperBlog.com

Home | Contact Us

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

HiveQL - GROUP BY and HAVING Clause

HiveQL - GROUP BY and HAVING 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 - GROUP BY and HAVING Clause

The Hive Query Language provides GROUP BY and HAVING clauses that facilitate similar functionalities as in SQL. Here, we are going to execute these clauses on the records of the below table:

HiveQL - GROUP BY and HAVING Clause

GROUP BY Clause

The HQL Group By clause is used to group the data from the multiple records based on one or more column. It is generally used in conjunction with the aggregate functions (like SUM, COUNT, MIN, MAX and AVG) to perform an aggregation over each group.

Example of GROUP BY Clause in Hive

Let's see an example to sum the salary of employees based on department.

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

HiveQL - GROUP BY and HAVING 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 - GROUP BY and HAVING Clause
  • Load the data into the table.
hive> load data local inpath '/home/codegyani/hive/emp_data' into table emp;

HiveQL - GROUP BY and HAVING Clause
  • Now, fetch the sum of employee salaries department wise by using the following command:
hive> select department, sum(salary) from emp group by department;

HiveQL - GROUP BY and HAVING Clause
HiveQL - GROUP BY and HAVING Clause
HiveQL - GROUP BY and HAVING Clause

Here, we got the desired output.

HAVING CLAUSE

The HQL HAVING clause is used with GROUP BY clause. Its purpose is to apply constraints on the group of data produced by GROUP BY clause. Thus, it always returns the data where the condition is TRUE.

Example of Having Clause in Hive

In this example, we fetch the sum of employee's salary based on department and apply the required constraints on that sum by using HAVING clause.

  • Let's fetch the sum of employee's salary based on department having sum >= 35000 by using the following command:
hive> select department, sum(salary) from emp group by department having sum(salary)>=35000;

HiveQL - GROUP BY and HAVING Clause
HiveQL - GROUP BY and HAVING Clause
HiveQL - GROUP BY and HAVING Clause

Here, we got the desired output.





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