Geek Slack

My SQL Tutorial
About Lesson


MySQL HAVING Clause


MySQL HAVING Clause

The HAVING clause in MySQL is used to filter the results returned by a GROUP BY clause based on specified conditions. It is similar to the WHERE clause but is applied after the grouping is done.

Example:

SELECT department_id, COUNT(*) AS employee_count
FROM employees
GROUP BY department_id
HAVING COUNT(*) > 5;

This query calculates the number of employees in each department and filters out departments with more than 5 employees using the HAVING clause.

The HAVING clause is particularly useful when you want to filter grouped data based on aggregate function results.

Join the conversation