Impala HAVING Clause with Syntax & Restrictions
1. Objective – HAVING Clause
In our previous, tutorial we learn Impala LIMIT Clause. Here, we are going to learn the Impala Having Clause. This is used to specify conditions that filter which group results appear in the final results in Impala. Moreover, we will see the introduction, syntax, type, and example, to understand it well.
So, let’s discuss Impala Having Clause.
2. Impala HAVING Clause
Basically, Impala HAVING Clause examines the results of aggregation functions rather than testing each individual table row then performs a filter operation on a SELECT query. Hence, we can say we use it in conjunction with functions always. Such as COUNT(), SUM(), AVG(), MIN(), or MAX(). Also, with the GROUP BY clause, typically.
Let’s see Impala GROUP BY Clause
In other words, it enables you to specify conditions that filter which group results appear in the final results in Impala.
However, we use this clause along with the Group By Clause. So, that places conditions on groups created by the GROUP BY Clause.
a. The syntax of HAVING Clause
So, the syntax for using Impala HAVING Clause is-
select * from table_name ORDER BY col_name [ASC|DESC] [NULLS FIRST|NULLS LAST]
b. Restrictions in Impala Having Clause
There is one restriction while using Impala HAVING Clause is that here filter expression cannot include a scalar subquery.
3. Example of Having Clause in Impala
Let us suppose we have a table named Employees in the database my_db. Its contents are −
[quickstart.cloudera:21000] > select * from Employees;
Query: select * from Employees
Fetched 9 row(s) in 0.51s
Read about Impala OFFSET Clause with Example
So, now let’s see an example of using Having clause in Impala −
[quickstart.cloudera:21000] > select max(salary) from Employees group by age having max(salary) > 20000;
This query displays those salaries, which are greater than 20000, but initially, it groups the table by age and selects the maximum salaries of each group.
Fetched 4 row(s) in 1.30s
So, this was all about Impala Having Clause. Hope you like our explanation.
Hence, we have seen the whole concept of Impala HAVING Clause. Moreover, we discussed syntax and example of Having Clause in Impala. Still, if any doubt occurs, feel free to ask in the comment section.
Related Topic- Impala WITH Clause