×

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

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

SQL SELECT MIN

The SQL Min() function is an aggregate function in SQL. SQL Min() returns the minimum value of a given condition. The expression may be numerical, or it may be an expression. The syntax...

5 minutes read.

SQL SELECT BETWEEN Operator

This SQL tutorial explains and helps us understand how to use the BETWEEN operator in the SELECT query with examples. The SQL SELECT BETWEEN operator retrieves the data from the table...

2 minutes read.

SQL Alter Table

In Structured Query Language, if you want to add columns in an existing table, then modify the table, or delete columns from the table. All these operations are allowed only...

7 minutes read.

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples. SQL Aggregate Function is a function used to perform calculation operations on one...

4 minutes read.

SQL Data Control Language

Data Control Language decides to whom should (which user) permit access privileges. GRANT and REVOKE are the commands of DCL. GRANT: It gives privileges to user. REVOKE: It takes back privileges from granted...

1 minute read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

What is SQL Injection?

Introduction to SQL Injection SQL injection is a vulnerability or a technique that might destroy the database of a website or a web application. It is one of the most widely...

4 minutes read.

TCL Commands in SQL

In Structured Query Language, TCL is an abbreviation for Transaction Control Language. A single unit of work in a database is formed after the consecutive execution of commands is known...

6 minutes read.

SQL UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

3 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 Temporary Tables

Temporary Table: The Temporary tables are generally created in TempDB. If the last connection is terminated then the tables are deleted automatically. These are generally used to store and process the...

4 minutes read.

SQL Left Join

The SQL Left Join query displays all the records from the table and displays similar records from the right table. The query displays zero records if it doesn’t find any...

4 minutes read.

GROUP BY vs ORDER BY

The GROUP BY clause and ORDER BY clause in SQL are used to arrange data obtained by SQL queries. The important difference between the GROUP BY clause and ORDER BY...

4 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

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 Operators

Arithmetic Operators Arithmetic operators are +, -, *, /, % performs addition, subtraction, multiplication, division, modulo respectively. Example:   Select 100+222; Select salary+100 From teacher Where teacher_id=1; Following output screen shows different arithmetic operations. We...

2 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.

Trigger in SQL

In this article, we will learn about the concept of trigger in SQL and its implementation with the help of an example. A Trigger in Structured Query Language is a set...

4 minutes read.

SQL CREATE TABLE

In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE...

5 minutes read.