• You cannot refer to the aggregate function count(all) in your where clause, because the groups have not yet been generated at the time the where clause is evaluated. Instead, you must put your group filter conditions in the having clause. When grouping data, you also can apply filter conditions to the data after the groups have been generated. The having clause is where you should place these types of filter conditions.

Grouping via Expressions

mysql> SELECT extract(YEAR FROM rental_date) year, 
-> COUNT(*) how_many 
-> FROM rental 
-> GROUP BY extract(YEAR FROM rental_date);

Generating Rollups