Filtering Grouped Results

HAVING Clause: After using GROUP BY, you can filter groups with the HAVING clause, which applies conditions to grouped results.Example:

SELECT department_id, AVG(salary) AS avg_salary
FROM employees
GROUP BY department_id
HAVING AVG(salary) > 60000;

Explanation: This retrieves departments where the average salary exceeds 60,000. Unlike WHERE, HAVING is applied after the grouping.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *