Skip to content
Edition NigeriaGlobal
Country: Philippines

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

Set up your profile

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

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 minProjects, consulting & analysisWhat business analysis isRead nowWhat business analysis isProjects, consulting & analysis · Intermediate · 15 minAI at workFact-checking AI-assisted contentRead nowFact-checking AI-assisted contentAI at work · Intermediate · 15 minMacro & global marketsReading economic data releases l…Read nowReading economic data releases like an investorMacro & global markets · Intermediate · 15 minTrading: risk, costs and processTick sizes and why they matterRead nowTick sizes and why they matterTrading: risk, costs and process · Advanced · 15 minInvesting strategiescontrarian investingRead nowWhat is contrarian investing?Investing strategies · Advanced · 15 minAI & securityCrypto custody and exchange secu…Read nowCrypto custody and exchange securityAI & security · Advanced · 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 min
English