Skip to content
CodeItRaw
Path

Summarising rows: COUNT, SUM, AVG, GROUP BY and HAVING to filter groups.

Key ideas

  • GROUP BY splits rows into buckets; COUNT, SUM and AVG fold each bucket into one row.
  • WHERE filters rows before grouping, HAVING filters groups after.
  • Every column in SELECT is either grouped or inside an aggregate.

Pattern

SELECT dept_id, COUNT(*) AS n, AVG(salary)
FROM employees
GROUP BY dept_id
HAVING COUNT(*) > 2;

Finish these first:SQL: Select and Filter

Lessons

Each lesson explains one idea from zero and ends with a few tasks. Lessons open in order; the explanation can be read at any time.

  1. 01

    Counting and grouping

    0/2 tasks

  2. 02

    Averages, totals and HAVING

    0/3 tasks

Boss

  • 03

    Top seller

    SQL query

  • The boss opens when every lesson of the topic is finished. Solve it too and the topic is complete, and the topics after it open.