SQL grouping and group filters 1 question in 2 chapters

  1. 01Aggregation semantics
  2. 02Sources

Aggregation semantics

  • 1.1 How do GROUP BY and HAVING differ, and where does WHERE fit? Medium 2 min

    They do different jobs rather than being alternatives. Group by collects rows with matching keys into groups, while having filters those groups after aggregation. Where filters individual rows before that happens. For example, I first keep paid orders, then group them by customer, and finally keep customers whose total exceeds a threshold. Moving that total condition into where would be invalid at the same query level.

    Read the full answer

Sources