Grouping Data

Group by

GROUP BY groups the data fetched by SELECT The query finds the customer_id values in the payment table, collects the ‘amount’ values of those with the same customer__id value, and groups them according to the customer_id. In other words, it returns the sum of the amount values of the rows with a specific customer_id value. Group by

Having

The having clause specifies a search condition for a group or cluster. HAVING is often used with the GROUP BY statement to filter groups or clusters based on a certain condition. NOTE: You cannot use column aliases in the HAVING statement because the HAVING clause is evaluated before the SELECT clause. Because column aliases specified Having

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. The query groups according to store_id and calculates how many customer_id rows or Having vs Where