EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Oracle Tutorial Oracle Having Clause
Secondary Sidebar
Oracle Tutorial
  • Advanced
    • Timestamp to Date in Oracle
    • Oracle Golden Gate
    • Oracle Virtual Machine
    • Oracle Describe Table
    • Oracle Clauses
    • Oracle Having Clause
    • Oracle?Primavera
    • Oracle FOREIGN Key
    • PIVOT in Oracle
    • Oracle Alter Table
    • Oracle Queries
    • Oracle Views
    • Oracle Window Functions
    • Oracle String Functions
    • Oracle Date Functions
    • Oracle Analytic Functions
    • Oracle Aggregate Functions
    • Select in Oracle
    • INSERT in Oracle
    • DISTINCT in Oracle
    • Function in Oracle
    • Oracle GROUP_CONCAT
    • Oracle INSTR()
    • Oracle CONVERT
    • Oracle LENGTH()
    • Oracle EXISTS
    • Oracle REPLACE()
    • Oracle MERGE
    • Oracle LEAD()
    • Oracle EXTRACT()
    • Oracle LISTAGG()
    • Oracle SYS_CONTEXT()
    • Oracle COALESCE
    • Oracle NVL()
    • Oracle SYSDATE()
    • Oracle?Date Format
    • Oracle SYS_GUID()
    • Oracle WILDCARDS
    • Oracle Synonyms
    • Oracle Subquery
    • BETWEEN in Oracle
    • FETCH in Oracle
    • Oracle Index
    • Oracle Function-based Index
    • Oracle UNIQUE Index
    • Oracle Bitmap Index
    • Oracle Column
    • Oracle Triggers
    • Oracle Procedures
    • Sample Database for Oracle
    • Oracle LIKE Operator
    • ORDER BY in Oracle
    • Oracle ORDER BY DESC
    • GROUP BY in Oracle
    • Oracle GROUP BY HAVING
    • Oracle Aliases
    • Table in Oracle
    • Oracle Temporary Table
    • Oracle? Table Partition
    • Oracle rename table
    • Oracle CTE
    • Cursor in Oracle
    • Oracle LOCK TABLE
    • Oracle Tablespace
    • Oracle CARDINALITY
    • Oracle REGEXP
    • Oracle REGEXP_REPLACE
    • Oracle to_date
    • JSON in Oracle
    • Oracle COMMIT
    • Oracle GRANT
    • Oracle MD5
    • Oracle ROLLBACK
    • Oracle Users
    • Oracle TIMESTAMP
    • IF THEN ELSE in Oracle
    • Oracle While Loop
    • Oracle Clone Database
    • Oracle Backup Database
    • Oracle? XML
    • Oracle XMLAGG
    • Oracle XMLTABLE
    • Oracle Performance Tuning
    • Oracle B Tree Index
    • Oracle fusion
    • Oracle ebs
    • Oracle GRC
    • Oracle ERP
    • Oracle ASM
    • Oracle Cloud
    • Oracle HCM Cloud
    • Oracle Integration Cloud
    • Oracle Jinitiator
    • Oracle pathfinder
    • Oracle VirtualBox
    • Oracle Weblogic Server
    • Oracle decode
    • Oracle Exadata
    • Oracle ZFS
    • Oracle? utilities
    • JDBC Driver for Oracle
    • Oracle? DBA Versions
    • Oracle DBA Salary
  • Basic
    • Oracle Marketing Cloud
    • What is Oracle?
    • Career in Oracle
    • How to Install Oracle
    • Oracle Versions
    • What Is Oracle Database
    • Oracle Data Warehousing
    • Oracle Warehouse Builder
    • Career In Oracle Database Administrator
    • Career In Oracle DBA
    • What is Oracle RAC
    • Oracle DBA
    • Oracle? Vanderbilt
    • What is RMAN Oracle
    • Oracle Database Administration
    • Oracle Operators
    • Oracle Constraints
    • Oracle number
    • Oracle Data Types
    • Oracle UNIQUE Constraint
    • Oracle Check Constraint
  • Joins
    • Joins in Oracle
    • Inner Join in Oracle
    • Oracle Cross Join
    • Left Join in Oracle
    • OUTER Join in Oracle
    • Oracle Full Outer Join
    • Natural Join in Oracle
    • Oracle Self Join
    • Oracle hash join
    • Oracle? Update with Join
  • Oracle SET Operators
    • UNION in Oracle
    • Oracle UNION ALL
    • INTERSECT in Oracle
    • MINUS in Oracle
  • Interview Questions
    • Oracle Interview Questions
    • Oracle Apps Interview Questions
    • Oracle Apps Technical Interview Questions
    • Oracle Database Interview Questions
    • Oracle Forms Interview Questions
    • Oracle PL/SQL Interview Questions
    • Oracle RAC Interview Questions
    • Oracle SOA Interview Questions

Related Courses

Oracle Course Training

Oracle DBA Certification Course

MongoDB Certification Training

Oracle Having Clause

By Priya PedamkarPriya Pedamkar

Oracle Having Clause

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.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (85,992 ratings)

Syntax 

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

The syntax is shown below:

SELECT aggregatefunction(expression) FROM TABLES [where condition] GROUP BY column/expression
HAVING condition;

Parameters

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

Query:

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.

oracle having clause

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

Query:

SELECT VEHICLE_NAME, SUM(sale)AS "TOTAL SALES" FROM vehicle GROUP BY VEHICLE_NAME
HAVING SUM(sale)<2000;

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.

 HAVING to select only the records

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.

Query:

SELECT VEHICLE_NAME,city ,MAX(sale)AS "MAX SALES" FROM vehicle GROUP BY VEHICLE_NAME, city
HAVING MAX(sale)<150;

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.

maximum value of less than 150

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.

Query:

SELECT VEHICLE_NAME,city ,MIN(sale)AS "MAX SALES" FROM vehicle GROUP BY VEHICLE_NAME, city
HAVING MIN(sale)>150;

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.

 opposite of MAX. MIN function

As you can see only the records having minimum sales more than 150 are displayed.

Conclusion

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.

Recommended Articles

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–

  1. Scope in Python
  2. Oracle LIKE Operator
  3. Types of Data Model
  4. INSERT in Oracle
  5. Learn the Different Oracle Versions
Popular Course in this category
Oracle Training (14 Courses, 8+ Projects)
  14 Online Courses |  8 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle DBA Database Management System Training (2 Courses)4.9
All in One Financial Analyst Bundle- 250+ Courses, 40+ Projects4.8
1 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more