← PostgreSQL learning path
PostgreSQL · Module 5 of 10

Aggregation and grouping

Summarise data with GROUP BY and aggregate functions.

📖 Key ideas

  1. Aggregate functions: COUNT, SUM, AVG, MIN, MAX.
  2. GROUP BY groups rows: SELECT city, COUNT(*) FROM users GROUP BY city.
  3. HAVING filters groups: HAVING COUNT(*) > 10.
  4. FILTER clause: COUNT(*) FILTER (WHERE active).
  5. 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