← PostgreSQL learning pathPostgreSQL · Module 5 of 10
Aggregation and grouping
Summarise data with GROUP BY and aggregate functions.
📖 Key ideas
- Aggregate functions: COUNT, SUM, AVG, MIN, MAX.
- GROUP BY groups rows: SELECT city, COUNT(*) FROM users GROUP BY city.
- HAVING filters groups: HAVING COUNT(*) > 10.
- FILTER clause: COUNT(*) FILTER (WHERE active).
- Window functions: ROW_NUMBER() OVER (PARTITION BY ...).
💡 Real-life example: GROUP BY sorts receipts into piles by shop; aggregates add up each pile.
COUNTCounts rows
GROUP BYGroups rows by values
HAVINGFilters groups
Window functionCalculation across related rows without collapsing them