×

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples.

The HAVING Clause in the SQL is used as we cannot use the WHERE clause condition with the aggregate function in the queries. The HAVING clause filters the result of GROUP BY based on the specified expression. The HAVING clause work is the same as the WHERE clause work helps to apply the expression in the query, but the HAVING clause works with the groups. To filter the group result, we use the HAVING clause.

Some important points to remember before using the HAVING clause:

  1. The HAVING clause is used with the SELECT query.
  2. In the query, the HAVING clause is mentioned after the GROUP BY clause.
  3. In the query, the ORDER BY clause is mentioned at the end after the HAVING conditions
  4. We use the HAVING Clause when we have a huge record.
  5. The HAVING clause applies the filter on the data according to the condition mentioned in the query.

The syntax of the HAVING clause is as follows:

SELECT Column_Name_1, Column_Name_2, Column_Name_3, Column_Name_4, Function_Name(Column_Name) FROM Table_Name GROUP BY Column_Name HAVING condition;

The function name is the aggregate function we will use in the query. For example, SUM(), MIN(), MAX(), COUNT(), AVG().

Consider the already existing table, which has the following data:

Table Name: D_Students

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592911
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202116Meena Mishra787580748577783
202117Mahesh Kumbhar758075788076775
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202220Prajwal Lokhande808585757880814
202221Anuja Wanare858886828485855
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078751
202224Aakash Jain807572748580784
202225Akshay Agarwal858078889082845
202226Shwetali Bhagwat908085889080861
202227Mayuri Wagh808085808285824
202228Utkarsh Rokade858080908484845
202229Manthan Koli857584788280812
202230Mayur Jain808887909290881

Example 1: Execute a query to display the student's information for a similar department id count greater than 3.

SELECT * FROM D_Students GROUP BY Department_Id HAVING COUNT(Department_Id)> 3;

We displayed the student's information whose department id count is similar, and the count is greater than 3 grouped by student_name in the above query.

The output of the above query is as follows:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592911
202112Vaibhav Lokhande859092808582862
202114Sonali Patole959092889290914
202117Mahesh Kumbhar758075788076775
HAVING Clause

Example 2: Execute a query to display students’ information where total minimum values are greater than 80 groups by student name.

SELECT * FROM D_Students GROUP BY Student_Name HAVING MIN(Total) > 80;

We displayed the student's information where the total column minimum value is greater than 80 groups by Student_Name in the above query.

The output of the above query is as follows:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202225Akshay Agarwal858078889082845
202221Anuja Wanare858886828485855
202115Axar Patel858082869284851
202229Manthan Koli857584788280812
202230Mayur Jain808887909290881
202227Mayuri Wagh808085808285824
202220Prajwal Lokhande808585757880814
202226Shwetali Bhagwat908085889080861
202114Sonali Patole959092889290914
202228Utkarsh Rokade858080908484845
202112Vaibhav Lokhande859092808582862
202111Vaishnavi Patil949188859592911
202222Venkatesh Iyer908987909291903
202113Yash Dhull908894878590893
HAVING Clause

Example 3: Execute a query to display student’s information where the sum of the first semester to the sixth-semester column is greater than 500 groups by Student_Name.

SELECT * FROM D_Students GROUP BY Student_Name HAVING SUM(First_Sem + Second_Sem + Third_Sem + Fourth_Sem + Fifth_Sem + Sixth_Sem) > 500;

We display the student's information where the sum of the first semester to the sixth-semester column is greater than 500 groups by Student_Name in the above query.

The output of the above query is as follows:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202225Akshay Agarwal858078889082845
202221Anuja Wanare858886828485855
202115Axar Patel858082869284851
202230Mayur Jain808887909290881
202226Shwetali Bhagwat908085889080861
202114Sonali Patole959092889290914
202228Utkarsh Rokade858080908484845
202112Vaibhav Lokhande859092808582862
202111Vaishnavi Patil949188859592911
202222Venkatesh Iyer908987909291903
202113Yash Dhull908894878590893
HAVING Clause

Related Topics

SQL UNION ALL Operator

This page has all the information about UNION ALL operator, which is used to join all the values from the SELECT queries. Similar records were not considered in the result in...

3 minutes read.

SQL Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

3 minutes read.

SQL Injection

SQL injection is a technique, this may destroy the database. It is one type of hacking technique. SQL IN WEB PAGES: Injection occurs when we ask for input like an id or...

3 minutes read.

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 minutes read.

SQL Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value. Here, multiple rows of data are...

4 minutes read.

SQL Syntax

In this tutorial, we will help you understand about the different SQL Syntax concepts with the help of an example. Every Structured Query Language starts with Keywords like select, insert, update,...

5 minutes read.

SQL SELECT Statement

The SQL SELECT statement is used to retrieve the data from the tables. We can also retrieve the selected records from the table using the query condition. The SQL SELECT...

5 minutes read.

SQL INTERSECT

SQL intersect operator is used to combine two or more SELECT statements, but it only displays the data similar to the SELECT statement. The syntax for the INTERSECT operation: SELECT COLUMN_NAME1, COLUMN_NAME2,...

3 minutes read.

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

9 minutes read.

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 minutes read.

SQL FOREIGN KEY

In this article, we will learn about the FOREIGN KEY constraints and how to define a FOREIGN KEY constraint to build the relationship between two tables. In a Relational Databases Management...

4 minutes read.

SQL Aliases

SQL Aliases: These are used to give a temporary name for a column or table. These are used to make tables or columns more readable. These are used to rename the table...

2 minutes read.

SQL SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

3 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

4 minutes read.

Nth highest salary

The most common and important question asked in interviews that how we can find the Nth highest salary in a table (2nd highest salary, 3rd highest salary, or Nth highest...

6 minutes read.

SQL Cast Operator

While writing SQL queries, we can see many values which are to be converted into another datatype for accessing them. In SQL this conversion is generally done by CAST function. CAST...

4 minutes read.

SQL INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help...

5 minutes read.

SQL KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst...

3 minutes read.