Skip to content
Edition NigeriaGlobal
Country: Mexico

You are in the Mexico edition. Type your country to switch.

The Americas

Set up your profile

You are learning in the Mexico edition, with examples in pesos and from Mexico.

Data skills: SQL and Python basics · Lesson 3 of 7

Summarising with SQL: GROUP BY, COUNT, SUM and AVG

Summaries turn many rows into a few answers: how many orders, how many units, how much revenue per region. This lesson covers COUNT, SUM and AVG, grouping with GROUP BY, the difference between WHERE and HAVING, and the order in which a database reads a query.

Beginner9 min read6 steps

This lesson is complete in writing. A video for it is on its way.

What you will learn

  • By the end you can summarise a whole table with COUNT, SUM and AVG.
  • You can write a GROUP BY query that returns one row per group, such as one per city.
  • You can choose correctly between WHERE and HAVING.
  • You can explain the order in which a database reads a query, and use it to fix common errors.

Key terms in this lesson

Aggregate function
A function that turns many values into one summary value, such as COUNT for how many rows, SUM for a total, AVG for an average, or MIN and MAX.
GROUP BY clause
The part of an SQL query that collects rows sharing a value, such as the same region, so that aggregate functions return one summary row per group.
HAVING clause
The part of an SQL query that filters groups after they are summarised, for example keeping only regions whose total revenue is above a chosen amount.
Logical query order
The order in which a database works through an SQL query: FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY and LIMIT, which differs from the order it is written in.
Pivot table
A spreadsheet tool that summarises a long table by groups, such as totals by year and sector.

Step 1 of 6

Summing up a whole table

Opening this step…

Step 2 of 6

One row per group with GROUP BY

This step opens when you finish the one above.

Step 3 of 6

Averages and what they hide

This step opens when you finish the one above.

Step 4 of 6

WHERE filters rows, HAVING filters groups

This step opens when you finish the one above.

Step 5 of 6

The order a query is read in

This step opens when you finish the one above.

Step 6 of 6

Checking a summary before you share it

This step opens when you finish the one above.

Key takeaways

The key takeaways open when you finish every step.

Lesson quiz

6 questions. You need 85% to pass.

The quiz opens when you finish every step.

Stockertim Learn is education. It explains how investing works and never recommends buying or selling anything.

Up next

Skills for the AI ageJoining tables: SQL JOIN for beg…Read nowJoining tables: SQL JOIN for beginnersSkills for the AI age · Beginner · 15 minSkills for the AI agePython basics: variables, lists,…Read nowPython basics: variables, lists, loops and functionsSkills for the AI age · Beginner · 15 minSkills for the AI agepandas basics and a first chartRead nowpandas basics and a first chartSkills for the AI age · Beginner · 15 minSkills for the AI ageAI as a coding partner: read, te…Read nowAI as a coding partner: read, test and fix its codeSkills for the AI age · Beginner · 15 minFinancial data & analyticsLab 4: SQL on a price tableRead nowLab 4: SQL on a price tableFinancial data & analytics · Advanced · 15 minAI at workNatural-language questions and SQLRead nowNatural-language questions and SQLAI at work · Intermediate · 15 minAI at workSpreadsheets and analysis helpRead nowSpreadsheets and analysis helpAI at work · Beginner · 15 minSkills for the AI ageSQL basics: SELECT, WHERE and OR…Read nowSQL basics: SELECT, WHERE and ORDER BYSkills for the AI age · Beginner · 15 minSkills for the AI ageIF, SUMIFS and COUNTIFSRead nowIF, SUMIFS and COUNTIFSSkills for the AI age · Beginner · 15 minSkills for the AI agePivot tables, charts and a simpl…Read nowPivot tables, charts and a simple dashboardSkills for the AI age · Beginner · 15 minSkills for the AI ageDesigning an agent: limits, huma…Read nowDesigning an agent: limits, human approval and logsSkills for the AI age · Advanced · 15 minSkills for the AI agePayroll reconciliation and an in…Read nowPayroll reconciliation and an inventory countSkills for the AI age · Beginner · 15 min
English