aımnova
DashboardMy LearningCrash CoursesStudy Plan

Aimnova site navigation

Stay in the loop

Get the latest study resources and updates

New features, study tips and exam insights — straight to your inbox.

IB Diploma

  • IB Past Papers
  • IB Study Notes
  • IB Question Bank
  • IB Mock Exams
  • IB Revision

IB Subjects

  • IB Math AA
  • IB Math AI
  • IB Economics
  • IB Business Management
  • IB Physics
  • IB Biology
  • View all IB subjects→

IB Past Papers

  • IB Math AA HL Past Papers
  • IB Math AA SL Past Papers
  • IB Math AI HL Past Papers
  • IB Math AI SL Past Papers
  • IB Economics HL Past Papers
  • IB Economics SL Past Papers
  • IB ESS Past Papers
  • View all past papers→

Study Resources

  • Study Notes
  • Question Bank
  • Mock Exams
  • Crash Courses
  • Flashcards
  • Revision Guide
  • Exam Skills
  • Command Terms
  • Grade Calculator
  • Exam Timetable 2026

Aimnova

  • Features
  • Pricing
  • For Teachers
  • For Schools
  • For Parents
  • About Us
  • Blog
  • Contact
aımnova

IB study, practice and feedback built around the real exam.

TermsPrivacyCookies·© 2026 Aimnova. All rights reserved.c1e448f

Aimnova is not affiliated with or endorsed by the International Baccalaureate Organization (IB).

NotesComputer Science HLTopic 3.3SQL aggregate functions
Back to Computer Science HL Topics
3.3.44 min read

SQL aggregate functions (Computer Science HL)

IB Computer Science · Unit 3

Your first topic is free to keep

Know exactly what to write for full marks

Practice with exam questions and get AI feedback that shows you the perfect answer — what examiners want to see.

Start Free

Contents

  1. 1Many rows in, one number out
  2. 2GROUP BY and HAVING
  3. 3Nulls, and what aggregates ignore
  4. 4Exam-style question
The big idea: An aggregate function collapses many rows into a single value.

COUNT how many · SUM the total · AVG the mean · MIN and MAX the extremes. Everything else in this topic is about controlling which rows they see.
SELECT COUNT(*)        AS orders,
       SUM(total)      AS revenue,
       AVG(total)      AS average_order,
       MAX(total)      AS biggest
FROM   Orders
WHERE  order_date >= '2025-01-01';
COUNT(*) and COUNT(column) differ: COUNT(*) counts rows. COUNT(column) counts rows where that column is not null.

After a LEFT JOIN this is the difference between a customer with no orders scoring 1 and scoring 0 — and it is the single most examined detail here.
AVERAGE in the guide, AVG in SQL: The IB guide lists the five aggregates as AVERAGE, COUNT, MAX, MIN, SUM. In SQL the average is written AVG(column). Grouped totals like these are what reports and managers' decisions are built on.

Free preview

This is the free notes preview

You're reading the free notes. Aimnova Pro unlocks the full study experience — and you can try it with your first topic free to keep:

  • FlashcardsLock in vocabulary and key terms with spaced repetition.
  • Practice questionsAnswer exam-style questions and get instant AI marking.
  • Mock exams & past-paper vaultSit full mocks and see exactly how examiners award marks.
  • Personalised study planA daily plan built around your exam date and weak areas.
Start Studying Free Full access to Aimnova Pro · cancel anytime
ClauseFiltersApplied
WHEREIndividual rowsBefore grouping
GROUP BY—Forms the groups
HAVINGWhole groupsAfter grouping
SELECT   region,
         COUNT(*)   AS orders,
         SUM(total) AS revenue
FROM     Orders
WHERE    order_date >= '2025-01-01'   -- rows, before grouping
GROUP BY region
HAVING   SUM(total) > 50000           -- groups, after grouping
ORDER BY revenue DESC;
Why WHERE cannot test an aggregate: WHERE is evaluated row by row, before any group exists. At that moment there is no total to compare against — so WHERE SUM(total) > 50000 is not merely wrong, it is meaningless.

A condition on an aggregate needs HAVING, always.
Every non-aggregated column must be grouped: If a column appears in SELECT and is not inside an aggregate, it must appear in GROUP BY.

Otherwise the database is being asked which of several values to show for a group, and there is no answer.

Study smarter, not longer

Most students waste 40% of study time on topics they already know. Our AI tracks your progress and optimizes every minute.

Try Smart Study FreeYour first topic is free to keep • No credit card required

The behaviour to know

  • AVG, SUM, MIN, MAX ignore nulls — they are skipped, not treated as zero
  • So AVG over 10 rows with 3 nulls divides by 7, not 10
  • That is usually what you want — but only if you know it is happening
  • COUNT(*) is the exception: it counts every row regardless
  • A null is not zero and not an empty string — it is the absence of a value
State the divisor: If a question gives a column with missing values, say explicitly what AVG divides by. "The average is over the 7 rows that have a value, not all 10" is frequently the mark.

How this is tested — you must place a condition in WHERE or HAVING correctly and choose COUNT's argument deliberately. It comes up two ways:

Paper 1 Section A

  • Construct a query using an aggregate, 3-5 marks
  • State the difference between WHERE and HAVING
  • Give the result of an aggregate over given data

Paper 1 Section B — case study

  • Write a grouped query with a condition on the total
  • Explain why a query returns the wrong count
The classic trap: Putting a condition on a total into WHERE. WHERE runs before any group exists, so there is no total yet — it must be HAVING.
IB-style questionConstruct[5 marks]

Tables: Customer(customerID, name, city) and Order(orderID, customerID, total). Construct SQL listing each city with its number of customers and total revenue, including cities whose customers have never ordered, for cities with more than 10 customers.

Model answer plan

See the answer broken into clear points, with guidance on how each point earns marks.

Claim your free topic

Try an IB Exam Question — Free AI Feedback

Test yourself on SQL aggregate functions. Write your answer and get instant AI feedback — just like a real IB examiner.

A query must list only the customers whose orders total more than 1000. why the condition SUM(total) > 1000 must be written in HAVING rather than WHERE. [2 marks]

Related Computer Science HL Topics

Continue learning with these related topics from the same unit:

3.1.1Relational databases3.2.1Database schemas3.2.2ERDs3.2.3Data types
View all Computer Science HL topics
Improve your exam techniqueCommand terms, paper structure, and mark-scheme tips for Computer Science HL
Previous3.3.3Updating data with SQLNextDatabase views3.3.5

4 questions to test your understanding

Reading is just the start. Students who tested themselves scored 82% on average — try IB-style questions with AI feedback.

Start FreeView All Computer Science HL Topics