Introduction to Oracle Having Clause
Oracle HAVING Clause is a non-compulsory conditional clause, which can be used along with the GROUP BY function as an aggregate option and this clause cannot be used on its own. HAVING clause on Oracle is a filter which is specific to the conditions under the GROUP BY statement, all in same query. A Few of the aggregation operations applied, under GROUP BY, as a part of HAVING clause are SUM, MAX, MIN and COUNT.
The syntax is shown below:
SELECT aggregatefunction(expression) FROM TABLES [where condition] GROUP BY column/expression
An aggregate function: This parameter represents the aggregate functions used in the query.
Example: COUNT, SUM, AVERAGE
- Expression: The expression on which we are applying the aggregate function is given under the parenthesis.
- Tables: The name of the tables from where we want the data to be extracted.
- Where Condition: This is optional. It is used if we want to put any condition for selecting the record.
- GROUP BY: This is used to group rows based on the expression/column which is followed by group by.
- HAVING Conditions: This specifies conditions which are applied to aggregated rows for filtering purpose.
How does the HAVING Clause work in Oracle?
So, now the question which should come to our mind is how does the HAVING clause work. We are going to discuss the same in this section.
HAVING clause is an optional clause that is in general applied after GROUP BY. HAVING does not work on rows of data like the WHERE clause but actually works on aggregated data or groups of data. So suppose we have a query which looks like the one below:
SELECT COUNT(*) FROM employee WHERE age>25 GROUP BY city HAVING COUNT(*)> 2;
So if we see we have the HAVING clause in this query. So when the query gets executed the FROM will get executed first to get the table from the database and then where the condition will get executed which will give us the rows which have age greater than twenty-five. Once we get that we will group the rows based on the city and then the HAVING clause will be applied so that only the groups which have cities with more than two entries get selected. Once we get that we will use an aggregate function to count the number of rows in the selected group.
So, through the above example, we got to know the working of a query with a HAVING clause.
To help us understand better we will go through a few examples.
We will use the various aggregate functions present in ORACLE as our examples.
1. HAVING with the COUNT function
COUNT is an aggregate function that is used to return the number of items present in a group. It includes both NULL as well as duplicates values. In this example below, we will see how to use COUNT with having
SELECT city, COUNT(*)AS "Number of customers" FROM employee GROUP BY city
HAVING COUNT(*)> 2;
In this example, the groups of cities are selected by the GROUP BY function and then we are using the HAVING clause with COUNT function to select the groups which have cities that have more than two records.
The below screenshot shows the output when executed in SQL developer.
As you can see in the output that it displays only those cities which have more than 2 records in the table.
2. HAVING with the SUM function
SUM function returns the summed value of the column or expression whichever is provided inside the parenthesis. In this example below, we are going to see how to use HAVING clause with the SUM function
SELECT VEHICLE_NAME, SUM(sale)AS "TOTAL SALES" FROM vehicle GROUP BY VEHICLE_NAME
In the above query, we are displaying the vehicle names along with their total sales. The group by function creates a group based on the vehicle names and then we are using HAVING to select only the records where the summed value of a sale is less than 2000.
The below screenshot shows the output of the query when executed in SQL developer.
As you can see the output only displays the vehicle names whose total sales are less than 2000.
3. HAVING with MAX function
MAX function returns the maximum value of a set. It ignores the null values of the set.
SELECT VEHICLE_NAME,city ,MAX(sale)AS "MAX SALES" FROM vehicle GROUP BY VEHICLE_NAME, city
In the above query, we are trying to find the vehicle name and city along with maximum sale which is less than 150. So first the group by function is used to group the records on the basis of vehicle name and city and then by using the HAVING clause we allow only those groups to be selected which has a maximum value of less than 150.
On executing the query in SQL we get the below result.
As you can see only the records having sales less than 150 are displayed.
4. HAVING with MIN function
It is the opposite of MAX. MIN function returns the minimum value of a set.
SELECT VEHICLE_NAME,city ,MIN(sale)AS "MAX SALES" FROM vehicle GROUP BY VEHICLE_NAME, city
In the above query, we are trying to find the vehicle name and city along with minimum sale which is more than 150. So first the group by function is used to group the records on the basis of vehicle name and city and then by using the HAVING clause we allow only those groups to be selected which has a minimum value of more than 150.
On executing the query in SQL we get the below result.
As you can see only the records having minimum sales more than 150 are displayed.
In this article, we have learned about what is HAVING clause, how it works in terms of execution and also discussed various examples to get a detailed understanding of various aggregate functions that can be used with the HAVING clause.
This is a guide to Oracle Having Clause. Here we discuss how does the HAVING clause work in oracle and Parameters along with the Syntax and the queries examples. You can also go through our other suggested articles to learn more–