Search⌘ K
AI Features

Solution: Only Keep Interesting Groups

Understand how to refine grouped SQL query results by applying the HAVING clause to filter aggregates. Learn to calculate averages per group and display only groups meeting specific conditions, such as cities with an average age below 28.

Query

SELECT city, AVG(age) AS avg_age
FROM people
GROUP BY city
HAVING AVG(age) < 28;

Explanation

This query finds the average age of people in each city, but only shows cities where the average age is less than 28. ...