MySQL HAVING

Last Updated: March 14, 2022

HAVING clause is used to impose grouping level conditions

WHERE clause is for the row-level results whereas the HAVING clause is for the group-level results

You apply the GROUP BY function with the aggregate functions and get the group-level results then you can apply the HAVING clause to filter out the grouped data.

HAVING Syntax

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

Example

Student table with student_name, subject, and marks

groupby table 2

The following query will get the average marks of the students.

SELECT student_name, AVG(marks) FROM students
GrOUP BY student_name;
groupby result2 1

If you want to get the list of students whose average is greater than 70 then you can use the HAVING clause.

SELECT student_name, AVG(marks) FROM students
GROUP BY student_name
HAVING AVG(marks) > 70
having result
Test