WHERE filters rows at the data selection stage from the table, that is, before grouping. HAVING is applied after grouping and filters already grouped data. If you need to select rows before GROUP BY, use WHERE, and if after — HAVING. For example, to select groups with more than 2 elements, use HAVING.