Summarising rows: COUNT, SUM, AVG, GROUP BY and HAVING to filter groups.
Key ideas
GROUP BYsplits rows into buckets;COUNT,SUMandAVGfold each bucket into one row.WHEREfilters rows before grouping,HAVINGfilters groups after.- Every column in
SELECTis 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.
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.