Having vs Where

The WHERE clause filters row based on a specified condition. However, the HAVING clause filters groups of rows based on a specified condition.

In other words, the WHERE clause applies to rows, while the HAVING clause applies to the group of rows.

SELECT customer_id, SUM (amount) FROM payment GROUP BY
customer_id HAVING SUM (amount) > 200;

SELECT store_id, COUNT (customer_id) FROM customer GROUP BY
store_id;

The query groups according to store_id and calculates how many customer_id rows or how many rows with these store_id values (COUNT (customer_id) ). It gives the same result as the following query.

SELECT store_id, COUNT (*) FROM customer GROUP BY store_id;

SELECT store_id, COUNT (customer_id) FROM customer GROUP BY
store_id HAVING COUNT (customer_id) > 300;

In this query, after grouping by the store_id value and calculating the number of rows with this store_id, displays results where the total number of customer_id rows is greater than 300.

SELECT rental_id , min(amount) min_amount FROM payment GROUP
BY rental_id HAVING min(amount)>10 ORDER BY min(amount) ;

While the query is grouped by rental_id value, it calculates the minimum amount corresponding to these rental_id values. The minimum amount value displays rental_id values greater than 10.