Skip to the page
Year 10 · Computer Science · Summer 2 · Databases and the Year 10 mock

SUM and COUNT; validation in databases

Exam practicePresent ▶

What this lesson covers

9.4 SQL: ORDER BY, SUM and COUNT

Cambridge IGCSE Computer Science 0478 · Topic 9: Databases · Paper 2

9.2 Data types, validation and primary keys

Cambridge IGCSE Computer Science 0478 · Topic 9: Databases · Paper 2

You will learn to

  • Use SUM to total a numeric field and COUNT to count records.
  • Suggest a validation check for each field in a table, and justify it.
  • Answer past-paper database questions under time.

Success criteria

  • I can write a query that totals a field for records that meet a condition.
  • I can write a COUNT query and predict its answer.
  • I can name a suitable check for each field and say why.
The syllabus points for these sections
  • Sort query output with ORDER BY ASC and DESC.
  • Total a numeric field with SUM.
  • Count records with COUNT.
  • Complete and write full queries for exam scenarios.
  • Choose the best data type for a field: text/alphanumeric, character, Boolean, integer, real, date/time.
  • Suggest validation checks for fields in a table.
  • Explain what a primary key is.
  • Choose a suitable primary key and justify it.

Key words

Say each word, then read what it means. Every one is in the glossary too.

Database
An organised, structured store of data that a computer can search and sort.
Table
A grid of data made of rows (records) and columns (fields).
Field
One column of a table; it holds one item of data about every record, such as Minutes.
Record
One row of a table; it holds all the data about one thing, such as one bike hire.
Field name
The heading at the top of a column, such as HireID; it is not a record.
Data type
The kind of data a field can hold, such as integer, real or Boolean.
Text / alphanumeric
A data type for letters, digits and symbols, such as a name or a phone number.
Character
A data type for exactly one letter, digit or symbol, such as E or M.
Boolean
A data type with only two possible values, TRUE or FALSE.
Integer
A data type for whole numbers, such as 55.
Real
A data type for numbers with a decimal part, such as 5.50.
Date/time
A data type for a date, a time, or both, such as 05/03/2026.
Primary key
A field whose value is unique for every record, so it identifies each record.
Validation
An automatic check that data is sensible and follows the rules before it is accepted.
SQL
Structured Query Language: the language used to ask a database for data.
Query
A question to the database, written in SQL, that returns matching data.
ORDER BY
The SQL clause that sorts the output, ascending (ASC) or descending (DESC).
SUM
An SQL function that adds up the values in a numeric field.
COUNT
An SQL function that counts how many records meet a condition.

The explanation

Read it here, or open the study page below to go one step at a time with things to try.

9.4 SQL: ORDER BY, SUM and COUNT

ORDER BY: sorting the output

ORDER BY sorts the output by one field. It always comes last in the query.

ASC means ascending: smallest to largest, A to Z, oldest date to newest.

DESC means descending: largest to smallest, Z to A, newest date to oldest.

HireIDMinutes
H0240
H0418
H078
e.g.Example
Filter BikeType = 'M': H02 (40), H04 (18), H07 (8) Sort by Minutes, largest first: 40, 18, 8
SELECT HireID, Minutes
FROM BikeHire
WHERE BikeType = 'M'
ORDER BY Minutes DESC;
?Why?

If you leave out ASC or DESC, the database sorts in ascending order. Writing ASC anyway makes your answer clear to the examiner.

SUM: adding up a field

SUM adds up all the values in a numeric field, for the records that meet the condition.

The output is one single number, not a list of records.

Only use SUM on integer or real fields. You cannot add up text.

e.g.Example
Members (Member = TRUE): H01, H03, H04, H06 Costs: 2.50 + 1.20 + 0.90 + 3.00 2.50 + 1.20 = 3.70 3.70 + 0.90 = 4.60 4.60 + 3.00 = 7.60 Output: 7.60
SELECT SUM(Cost)
FROM BikeHire
WHERE Member = TRUE;
!Watch out

Put the field name inside the brackets: `SUM(Cost)`. Writing `SUM Cost` or `SUM(Member)` will not earn the mark.

COUNT: how many records match

COUNT counts how many records meet the condition. It does not add the values.

The output is one single whole number.

Here we write `COUNT(HireID)`. You may also see `COUNT(*)`, which counts every matching record in the same way.

e.g.Example
Electric bikes: H01, H03, H05, H06, H08 Count them: 1, 2, 3, 4, 5 Output: 5
SELECT COUNT(HireID)
FROM BikeHire
WHERE BikeType = 'E';
?Why?

SUM answers "how much?". COUNT answers "how many?". Read the question for those words to choose the right one.

Putting it all together

A full query can use WHERE with AND, then ORDER BY. Work through it with the three moves: filter, pick columns, sort.

Then test yourself in the toy below. Its BikeHire table has different fields: BikeID, Type, Station, Hours and Cost. Type a query, predict the output on paper first, then press Run query.

Try the example buttons, then change `ASC` to `DESC`, or `AND` to `OR`. Watch how the output changes.

StationMinutesCost
Moncloa555.50
Retiro402.00
Sol424.20
e.g.Example
Filter: non-members (H02, H05, H07, H08) whose cost is 2.00 or more H02 2.00 ✓ · H05 5.50 ✓ · H07 0.40 ✗ · H08 4.20 ✓ Pick columns: Station, Minutes, Cost Sort by Station A to Z: Moncloa, Retiro, Sol
SELECT Station, Minutes, Cost
FROM BikeHire
WHERE Cost >= 2.00 AND Member = FALSE
ORDER BY Station ASC;

There is something to try for this step in the IGCSE library.

Writing a query from a question

Exam questions describe what is needed in words. Turn each part of the sentence into one line of SQL.

"Show the hire ID and station" → SELECT. "from the bike-hire table" → FROM. "of hires under 20 minutes or from Moncloa" → WHERE. "highest hire ID first" → ORDER BY … DESC.

Copy field names and the table name exactly as they appear in the table.

HireIDStation
H07Atocha
H05Moncloa
H04Sol
H03Atocha
SELECT HireID, Station
FROM BikeHire
WHERE Minutes < 20 OR Station = 'Moncloa'
ORDER BY HireID DESC;
!Watch out

A query that mixes AND and OR can be read two ways. SQL does AND first. If you must mix them, put brackets around the part that should be done first.

9.2 Data types, validation and primary keys

The six data types

Every field has a data type. The data type tells the database what kind of data the field may hold.

Cambridge uses six data types for databases. Learn them with an example from `BikeHire`.

Data typeWhat it holdsBikeHire fieldExample
Text / alphanumericLetters, digits and symbolsStation, HireIDRetiro, H05
CharacterExactly one letter, digit or symbolBikeTypeE
BooleanOnly two values: TRUE or FALSEMemberTRUE
IntegerA whole numberMinutes55
RealA number with a decimal partCost5.50
Date/timeA date, a time, or bothHireDate05/03/2026
?Why?

The right data type lets the database do the right things. It can add up real numbers, sort dates in time order and reject a letter typed into an integer field.

Choosing a data type: the tricky cases

Ask two questions. Will I do maths with it? Is it a whole number or does it need decimals?

A phone number is text, not integer. It can start with 0 or +34, and you never add phone numbers together.

A field with only yes/no answers is Boolean. A field that always holds one single symbol, like a grade `A` or a size `M`, is character.

e.g.Example
Field: MobileNumber, value +34 612 345 678 → Text (it has + and spaces, no maths) Field: Minutes, value 55 → Integer (whole number of minutes) Field: Cost, value 5.50 → Real (money has decimals) Field: HelmetHired, value TRUE → Boolean (yes or no)
!Watch out

Do not answer "number" or "string". Use the exact Cambridge names: integer or real, text/alphanumeric. A vague answer like "number" usually scores 0.

Validation in a database

Validation is an automatic check that data is sensible before the database accepts it. It does not prove the data is correct.

You met these checks in topic 7: range, length, type, presence, format and check digit.

In a database, each check is set on a field. The database then refuses bad data when someone types it in.

FieldCheckRule
MinutesRangeBetween 1 and 240
MinutesTypeMust be an integer
HireIDFormatThe letter H followed by two digits
HireIDLengthExactly 3 characters
StationPresenceMust not be left empty
?Why?

A wrong value, like 5500 minutes, would ruin every total and count. Stopping it at the door is easier than finding it later.

The primary key

A primary key is a field that holds a unique value for every record. No two records can have the same value in that field.

It lets the database pick out exactly one record, with no confusion.

To choose one, look for a field where no value can ever repeat, and that is never empty. ID fields are made for this.

e.g.Example
Which field in BikeHire is the best primary key? Station: Sol appears 3 times → not unique ✗ Minutes: values could repeat on another day → not unique ✗ HireDate: 03/03/2026 appears twice → not unique ✗ HireID: H01 to H08, every value different → unique ✓ Answer: HireID, because each hire has its own ID that is never repeated.
!Watch out

In the exam, give the field name and the reason. "HireID" alone may only earn 1 mark. Add "because each value is unique, so it identifies each record".

Study it

Open the study page and go one step at a time.

Check yourself

One question at a time. The reason shows after each answer.

Saved on this device.