Filtering using HAVING

You can filter results of the GROUP BY clause by adding the HAVING clause.

For example, find the employees with an average salary of more than 140,000:

mysql>  SELECT emp_no, AVG(salary) AS avg FROM salaries GROUP BY emp_no HAVING avg > 140000 ORDER BY avg DESC;+--------+-------------+| emp_no | avg         |+--------+-------------+| 109334 | 141835.3333 || 205000 | 141064.6364 |+--------+-------------+2 rows in set (0.80 sec)

Get MySQL 8 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.