This guide is specifically for PostgreSQL syntax.
Intermediate-level SQL HAVING Clause practice exercises with solutions.
Exercise 1
Question: Find departments with more than 5 employees.
SELECT department, COUNT(*) AS employee_count FROM employees GROUP BY department HAVING COUNT(*) > 5;
HAVING filters groups after aggregation. WHERE filters rows before grouping. Use HAVING with aggregate functions.
Exercise 2
Question: Find products with average rating above 4.0 and at least 10 reviews.
SELECT product_id, AVG(rating) AS avg_rating, COUNT(*) AS review_count FROM reviews GROUP BY product_id HAVING AVG(rating) > 4.0 AND COUNT(*) >= 10;
Multiple conditions in HAVING are combined with AND/OR just like in WHERE clause.
PostgreSQL-Specific Notes
This page covers PostgreSQL syntax. Other databases may have different syntax for similar operations.