HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
SQL: SUM, COUNT, AND, OR and Reading Query Output
  1. 1Counting and adding with COUNT and SUM
  2. 2Joining conditions with AND and OR
  3. 3Reading, completing and correcting SQL scripts
  1. Computer Science
  2. /SQL: SUM, COUNT, AND, OR and Reading Query Output
  3. /Counting and adding with COUNT and SUM

Course 9.3·Lesson 1 of 3

Counting and adding with COUNT and SUM

A query can give one number instead of a list: how many records pass, or the total of a field. COUNT counts the records and SUM adds up a numeric field. You use both with and without WHERE and work out the single value a script outputs.

about 25 min1 simulation1 practice question
Lesson 1 of 3Show all lessonsHide
SQL: SUM, COUNT, AND, OR and Reading Query Output
  1. 1Counting and adding with COUNT and SUM
  2. 2Joining conditions with AND and OR
  3. 3Reading, completing and correcting SQL scripts

Try the code and the simulations beside the lesson.

One number as the answer

A theatre manager does not want a list of every sale. She asks: how many sales were there on Saturday, and how much money did they bring in? Each answer is one number.

Here is the theatre's table, Tickets. The Amount field is in dollars. We use the table in all three lessons of this course.

table
SaleID  Day       Show     Seats  Amount
T1      Friday    Matinee  4      32
T2      Saturday  Evening  2      30
T3      Saturday  Matinee  6      48
T4      Sunday    Evening  3      45
T5      Saturday  Evening  5      75
T6      Friday    Evening  1      15

COUNT

COUNT(*) counts the records. The star means "count every record", whatever is in its fields.

sql
SELECT COUNT(*)
FROM Tickets;

The output:

table
COUNT(*)
6

With WHERE, it counts only the records that pass.

sql
SELECT COUNT(*)
FROM Tickets
WHERE Day = 'Saturday';

The output:

table
COUNT(*)
3

You may also meet COUNT(Field), such as COUNT(SaleID). It counts the records that have a value in that field. When no field is empty, as in this course, it gives the same number as COUNT(*).

SUM

SUM(Field) adds up the values of a numeric field.

sql
SELECT SUM(Seats)
FROM Tickets;

The output:

table
SUM(Seats)
21

With WHERE, it adds up only the records that pass. Here are the Saturday takings.

sql
SELECT SUM(Amount)
FROM Tickets
WHERE Day = 'Saturday';

The output:

table
SUM(Amount)
153

Check it by hand: the Saturday sales are 303030, 484848 and 757575 dollars, and 30+48+75=15330 + 48 + 75 = 15330+48+75=153 dollars.

The Tickets table drawn as a grid with the fields SaleID, Day, Show, Seats and Amount. The three Saturday rows, T2, T3 and T5, are shaded green. Two arrows lead from the table to two boxes: 'SELECT COUNT(*) ... WHERE Day = Saturday' gives 3, and 'SELECT SUM(Amount) ... WHERE Day = Saturday' gives 30 + 48 + 75 = 153.

COUNT or SUM?

  • "How many sales?" counts records: COUNT(*).
  • "How many seats?" adds the Seats field: SUM(Seats).

These two questions sound alike but give different numbers. On Saturday there were 333 sales, but 2+6+5=132 + 6 + 5 = 132+6+5=13 seats.

SUM needs a field of numbers, such as an integer or a real field. Adding up names makes no sense.

When nothing passes. If no record meets the condition, COUNT(*) gives 000. SUM gives an empty value (called NULL), which Python prints as None.

Practise with new numbers

Try it in Python

The program builds the Tickets table and prints four single numbers: two counts and two sums. fetchone()[0] takes the one value out of the one row of output.

Read it first. Then run it. Do you get 6, 3, 21 and 153?

1. Change 'Saturday' to 'Friday' in both scripts. Predict the count and the sum, then run it.

2. Change the day to 'Monday'. What does SUM print now?

Common mistakes

  • COUNT when the question wants a total. "How many seats were sold?" is SUM(Seats), not COUNT(*).
  • SUM on a text field. Only numbers can be added.
  • Forgetting the WHERE. Without it, the whole table is counted or added.
  • Writing the list instead of the number. COUNT and SUM output one value.

Remember these

  • COUNT(*) counts the records that pass.
  • SUM(Field) adds up a numeric field of the records that pass.
  • Each gives one value as the output.

Check yourself

Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.

  1. What does SELECT COUNT(*) FROM Tickets WHERE Show = 'Matinee'; output?
  1. What does SELECT SUM(Seats) FROM Tickets WHERE Show = 'Evening'; output?
  1. Write a script that gives the total amount taken on Friday.
  1. The manager asks "how many sales were for more than 333 seats?" Which function do you use?
  1. Why does SUM(Day) make no sense?

References

  • Cambridge International, IGCSE Computer Science 0478 syllabus (exams in 2026, 2027 and 2028), section 9 Databases, 9.4 (SUM and COUNT).

Enroll free in SQL: SUM, COUNT, AND, OR and Reading Query Output

Every lesson is open to read. Enrolling also lets you:

  • Mark lessons done and carry on where you stopped.
  • Take the tests of this lesson and its course.
  • Count the course towards the certificate for its chapter.
No payment. You can leave at any time.

Problems (3)

A café keeps its orders in a table Orders. Cost is in dollars. Give the output of each script.Easy · High School (9–10) · by StemPy Lab
A school kitchen records each lunch order in a table Lunches…Medium · High School (9–10) · by StemPy Lab
A bus company stores each trip of one morning in a table Fares…Medium · High School (9–10) · by StemPy Lab
Next: lesson 2Joining conditions with AND and OR
← SQL: SUM, COUNT, AND, OR and Reading Query Output
← PreviousJoining conditions with AND and OR →

Discussion (0)

Log in to join the discussion.

Loading…

1 Python program
5 questions to check yourself

This lesson is free to read. Enroll free to keep your place and take the tests.

Python Labs (1)

Counting and adding up salesPython Lab · by StemPy Lab