SQL GROUP BY Clause

Go back

There are cases when we want to apply a calculation to a group. For instance, if we want the average age by genre.

SELECT gender, AVG(age) FROM customer 
GROUP BY gender

We can't use calculations in the where, as they are only applied to groups. However, we can use the HAVING clause, to only keep groups based on a predicate.

-- keep groups for which average >= 20
SELECT gender, AVG(age) FROM customer 
GROUP BY gender HAVING AVG(age) >= 20