COUNT, SUM, AVG with GROUP BY and HAVING
So far every query returned rows from the table. Now we ask summary questions: how many orders were paid? How much money did each customer bring in? Who ordered more than once? These are the most common interview questions after JOIN, and their standing trap is the difference between WHERE and HAVIN
An aggregate function turns many rows into one number. GROUP BY does it for each group separately, and HAVING filters the groups.
- Aggregate function
- A function that takes many rows and returns one value, such as COUNT, SUM or AVG.
- GROUP BY
- A clause that splits rows into groups by a shared value and returns one row per group.
- HAVING
- A clause that filters groups after aggregation, for example only groups with more than two rows.