This lesson demonstrates how to use the HAVING clause.

We'll cover the following

Having Clause

We are familiar with the WHERE clause that can be used to filter rows. In the same way, the HAVING clause allows us to filter groups. At times, the HAVING clause can be used to filter rows to display but that is not the intended use and can make the query slower. The HAVING clause should be used to decide what rows form each group. Remember the HAVING clause works on groups of rows whereas the WHERE clause works on individual rows. We’ll work with a couple of queries in this lesson to clarify the concept.

Example Syntax

SELECT col1, AggregateFunction(col3) AS count

FROM table

GROUP BY col1, col2, … coln

HAVING count > 75

ORDER BY col2;

Connect to the terminal below by clicking in the widget. Once connected, the command line prompt will show up. Enter or copy and paste the command ./DataJek/Lessons/24lesson.sh and wait for the MySQL prompt to start-up.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.