Syllabus coverage
- Define a single-table database from given data storage requirements, including fields, records and validation
- Suggest suitable basic data types: text/alphanumeric, character, Boolean, integer, real and date/time
- Understand the purpose of a primary key and identify a suitable primary key for a given database table
- Read, understand and complete SQL scripts that query data stored in a single database table
- Use SELECT, FROM, WHERE, ORDER BY ASCENDING, ORDER BY DESCENDING, AND, OR, COUNT, SUM and AVERAGE
- Identify the output produced by an SQL statement applied to the given contents of a table
Part of the O Level Computer Science syllabus 2210.
The shape of a single-table database
A database is an organised store of data that can be searched, sorted and updated without rewriting it. A single-table database keeps everything in one table: a grid in which each field is a column and each record is a row. Every record describes one thing - one bike, one booking, one pupil - and holds one value in every field.
Take a bike-hire company. Its table might be called Bike and have the fields BikeID, Model, FrameSize, HourlyRate, InService and LastServiced. One record is one bike: B104, Hybrid, 54, 6.50, TRUE, 14/02/2026. The table has the same shape all the way down, which is what makes it possible to search it by any field.
The design of the table - the list of fields and the data type of each - is settled before any data is entered. Changing it later means rebuilding the table and converting every record, so the design is where the care goes. There are no links to other tables in this topic: every field the data needs must be in this one table.
| Term | What it means | In the bike table |
|---|---|---|
| Table | The whole grid of fields and records, holding one kind of thing | Every bike the company owns |
| Field | One item of data about the thing a record describes; a column, defined once | Model, HourlyRate, InService |
| Record | One complete row, with a value in every field | B104, Hybrid, 54, 6.50, TRUE, 14/02/2026 |
| Data storage requirement | The description of what must be stored, which you turn into fields | 'we need each bike's code, model, size, rate and whether it is available' |
From a storage requirement to a field list
A question that asks you to define a database from given data storage requirements gives you a paragraph describing what has to be kept, and expects a list of fields with a data type for each. Work through the description and write down one field for every separate item of data it names.
Each field holds exactly one item. A field called BikeAndOwner cannot be sorted by owner or searched by bike, because the two values are stuck together in one cell. Split it into BikeID and OwnerName, and each can be used on its own. The same applies to an address: HouseNumber, Street, Town and Postcode are four fields, and only then can the table list everyone in one town.
Field names are single words with no spaces, written so their meaning is obvious - DateOfHire rather than DOH. The order of the fields does not change what the database can do, but it does change how easy the table is to read when you are working out a query's output by hand, so keep related fields together.
Finish by deciding which fields must have a value in every record. A field that can never be left empty is a design decision, and it is enforced by a validation rule rather than by hope.
- One field for each separate item of data named in the requirement - count them and check the numbers match.
- One item per field: split combined fields such as NameAndAddress into their parts.
- Names are single words with no spaces and an obvious meaning.
- Settle the field list before entering data; changing it later means rebuilding the table.
Choosing a suitable data type
Every field has a data type, which decides what values it can hold and what can be done with them. The syllabus names six: text/alphanumeric, character, Boolean, integer, real and date/time. The test is always the data itself, not the name of the field.
Text/alphanumeric holds any combination of letters, digits and symbols and is never used in arithmetic: a model name, an address, a registration. Character holds exactly one character: a size code such as S, M or L, or a single letter grade. Boolean holds TRUE or FALSE and nothing else: whether a bike is in service, whether a fee has been paid.
Integer holds whole numbers: a frame size in centimetres, a number of gears, a quantity. Real holds numbers with a fractional part, so it is used for money and measurements: an hourly rate of 6.50, a distance of 12.4. Date/time holds a date, a time or both in a fixed format, so the database can put records in date order and work out how long ago something happened.
Two choices catch students out. A rate or price is real, because 6.50 has a fractional part that an integer would drop. A telephone number or bike code is text/alphanumeric even though it looks numeric, because it is never added up and it may begin with a zero, which an integer would remove.
| Data type | What it can hold | Suitable for |
|---|---|---|
| text/alphanumeric | Any letters, digits and symbols in any combination | Model, OwnerName, BikeID |
| character | Exactly one character | FrameSizeCode S, M or L; a single grade |
| Boolean | TRUE or FALSE only | InService, FeePaid, IsAvailable |
| integer | A whole number, positive or negative | NumberOfGears, Quantity, YearOfMake |
| real | A number with a fractional part | HourlyRate, Distance, Weight |
| date/time | A date, a time, or both | LastServiced, DateOfHire, TimeOut |
The primary key
A primary key is a field that uniquely identifies each record. No two records may hold the same value in that field, and no record may leave it empty. Its purpose is to make every record findable and distinguishable: without it, two bikes of the same model and size would be indistinguishable, and a query could not reliably return the one that was hired.
A suitable primary key has three properties. Its value is unique to each record, it is present in every record, and it never changes during the life of the record. A code or ID that the organisation assigns satisfies all three, which is why tables almost always have a field such as BikeID, MemberID or OrderNumber.
Fields that describe the thing are poor keys. Model is not unique, because the company owns several bikes of the same model. OwnerName is not unique, because two owners can share a name. LastServiced is not unique, because several bikes can be serviced on the same day. If nothing in the table is unique, the answer is to add a new field - a code or an ID - rather than to force a key onto a field that cannot carry it.
The primary key also speeds up searching: the database can go straight to the record with a given key instead of reading every record in turn. That is a benefit of the key beyond simply telling records apart.
- Unique to each record - no two records share a value.
- Present in every record - a key can never be blank.
- Never changes - a record keeps the same key for its whole life.
- Usually a code or ID, not a description such as a name, model or date.
Validation in a single-table database
Validation is an automatic check that data entered into a field is sensible before it is stored. It cannot tell whether a value is correct - a hire rate of 9.50 typed as 6.50 passes every check - but it stops the values that could not possibly be right.
A range check rejects a value outside stated limits: an HourlyRate below 0 or above 50, a FrameSize below 40 or above 62. A type check rejects the wrong kind of data, such as letters in an integer field. A presence check rejects a blank field that must have a value, such as BikeID. A length check rejects a value with too many or too few characters, such as a BikeID that must be exactly four characters. A format check rejects a value that does not match the expected pattern, such as a date that is not DD/MM/YYYY.
Validation rules belong to the design of the table, so they are written down with the fields. A question that asks for the design of a database often accepts a validation rule as part of the answer, and one example of a value that would be rejected proves you understand the rule.
| Check | What it does | Example in the bike table |
|---|---|---|
| Range check | Rejects a value outside stated limits | HourlyRate must be between 0 and 50 |
| Type check | Rejects data of the wrong type | NumberOfGears must be a whole number |
| Presence check | Rejects a blank field | BikeID cannot be empty |
| Length check | Rejects a value of the wrong length | BikeID must be exactly 4 characters |
| Format check | Rejects a value that does not match a pattern | LastServiced must be DD/MM/YYYY |
SELECT, FROM and WHERE
SQL queries a table and returns a result table. The three clauses that every query needs are SELECT, FROM and WHERE, and each answers a different question.
SELECT lists the fields that appear in the output. `SELECT Model, HourlyRate` returns those two columns; `SELECT *` returns every field. FROM names the table the data comes from: `FROM Bike`. WHERE gives a condition, and only the records for which the condition is true are returned: `WHERE HourlyRate < 10.00`.
The clauses are written in this order, each on its own line, and the statement ends with a semicolon in most conventions:
SELECT Model, HourlyRate FROM Bike WHERE InService = TRUE;
WHERE does not change the table - it filters the records that the query returns. Records that fail the condition are left out of the result, but they remain in the table. This is the single most useful fact for predicting output: apply WHERE first, and everything else to what survives.
Combining conditions with AND and OR
Two conditions can be combined in a WHERE clause. AND returns a record only when both conditions are true. OR returns a record when at least one of them is true.
`WHERE HourlyRate < 10.00 AND InService = TRUE` returns only bikes that are both cheap and in service - a bike that is cheap but out of service fails the second condition and is not returned. `WHERE FrameSize = 54 OR FrameSize = 58` returns bikes of either size, which is twice as many records as either condition alone would give.
There is no limit to the number of conditions that can be joined, but each one must be complete: `WHERE HourlyRate < 10.00 AND > 5.00` is not valid, because the second part has no field name. Write each condition in full: `WHERE HourlyRate > 5.00 AND HourlyRate < 10.00`.
When a question mixes AND and OR, work through the records one at a time and write down whether each condition is true. Do not try to do it in your head - the examiner is testing whether you can apply the logic, not how quickly you can do it.
| Clause | Returns a record when |
|---|---|
| WHERE A AND B | Both A and B are true |
| WHERE A OR B | A is true, or B is true, or both are true |
| WHERE A AND B OR C | Apply the conditions as written; test each record against the whole expression |
Sorting the result with ORDER BY
ORDER BY puts the returned records in order. It is written after WHERE and names the field to sort on:
SELECT Model, HourlyRate FROM Bike WHERE InService = TRUE ORDER BY HourlyRate ASCENDING;
ASCENDING means smallest first - A to Z for text, earliest to latest for dates, lowest to highest for numbers. DESCENDING means largest first - Z to A, latest to earliest, highest to lowest. The syllabus writes both keywords in full, so `ORDER BY HourlyRate DESCENDING`, not `DESC`.
ORDER BY changes only the order of the records that WHERE has already selected. It never adds or removes a record, so a query with no WHERE and an ORDER BY returns the whole table, sorted. It also does not change the columns: the output still has exactly the fields named in SELECT.
If the sort field is not in the SELECT list, the records can still be sorted by it - the field is used for the order but does not appear in the output. This is a favourite trick in output questions, so read the SELECT list separately from the ORDER BY line.
COUNT, SUM and AVERAGE
These three functions replace a list of records with a single number. COUNT counts how many records were selected. SUM adds up the values in a numeric field. AVERAGE returns the mean of the values in a numeric field.
SELECT COUNT(BikeID) FROM Bike WHERE InService = TRUE;
This returns one number: how many bikes are in service. `SELECT SUM(HourlyRate) FROM Bike;` returns one number: the total of every rate in the table. `SELECT AVERAGE(HourlyRate) FROM Bike WHERE FrameSize = 54;` returns the mean rate of the size 54 bikes. COUNT can be used on any field, including text; SUM and AVERAGE only make sense on a numeric field.
COUNT counts records, not values, so `COUNT(BikeID)` and `COUNT(Model)` return the same number when no field is empty. SUM and COUNT are easy to swap under pressure: if the question asks 'how many', it is COUNT; if it asks 'how much in total', it is SUM; if it asks 'on average', it is AVERAGE.
A function in SELECT returns a single value, so the result table has one row and one column. Nothing else can be listed beside it unless the query groups the records, which is beyond a single-table query at this level.
| Function | Returns | Example |
|---|---|---|
| COUNT(field) | The number of selected records | SELECT COUNT(BikeID) FROM Bike WHERE InService = TRUE; |
| SUM(field) | The total of the values in a numeric field | SELECT SUM(HourlyRate) FROM Bike; |
| AVERAGE(field) | The mean of the values in a numeric field | SELECT AVERAGE(HourlyRate) FROM Bike WHERE FrameSize = 54; |
Working out what a query returns
An output question gives you a table and a statement and asks for the result. Read the statement in the order the database applies it, which is not the order it is written: FROM first, then WHERE, then the SELECT list, then ORDER BY.
Start with FROM and put your finger on the table. Apply WHERE to every record and cross out the ones that fail; with AND, one failure is enough to remove the record, and with OR, one success is enough to keep it. Then take only the fields named in SELECT, in the order they are named. Finally sort what is left by the ORDER BY field, remembering that the sort field need not appear in the output.
If SELECT contains COUNT, SUM or AVERAGE, stop before the sorting stage: the answer is a single number, not a list. Count or add the records that survived WHERE, and write the number on its own.
Write the answer as a small table with the selected field names as headings. That shows the examiner you have applied SELECT, and it makes the effect of WHERE and ORDER BY visible in the rows.
- FROM: identify the table and its records.
- WHERE: test every record and discard the ones that fail.
- SELECT: keep only the named fields, in the order named.
- ORDER BY: sort the surviving records; ASCENDING smallest first, DESCENDING largest first.
- If a function is used, the answer is one number rather than a list.
Key terms you must be able to define
- Database
- An organised store of data that can be searched, sorted and updated without being rewritten by hand.
- Table
- A grid of fields and records holding one kind of thing; a single-table database has exactly one.
- Field
- One item of data about the thing a record describes. It is a column, defined once for the whole table.
- Record
- One complete row of a table, holding one value in every field and describing one thing.
- Primary key
- A field whose value is unique to each record, present in every record and never changed, so it identifies the record.
- Validation
- An automatic check that data is sensible before it is stored, such as a range, type, presence, length or format check.
- SQL
- Structured query language: the language used to query a database table and return a result table.
- WHERE
- The SQL clause that gives a condition; only records for which the condition is true are returned.
- ORDER BY
- The SQL clause that sorts the returned records, ASCENDING from smallest to largest or DESCENDING from largest to smallest.
In context
2210 sets its questions inside systems a student already uses - a school network, a phone, a supermarket till, a vending machine, a website. These show how the theory is applied to a context you have not seen before, then you answer for the system you are given.
The table Bike holds BikeID, Model, FrameSize, HourlyRate, InService and LastServiced. `SELECT BikeID, Model FROM Bike WHERE InService = FALSE ORDER BY LastServiced ASCENDING;` returns the bikes that are out of service, oldest service date first, so the workshop knows which to look at next.
One table, Loan, holds LoanID, BookTitle, BorrowerName, DateBorrowed and Returned. LoanID is the primary key because two borrowers can take the same title on the same day. `SELECT COUNT(LoanID) FROM Loan WHERE Returned = FALSE;` gives the number of books still out, and `SELECT BookTitle FROM Loan WHERE Returned = TRUE ORDER BY DateBorrowed DESCENDING;` lists the most recently returned titles first.
The table Member holds MemberID, Surname, DateJoined, SubscriptionPaid and Age. MemberID is the key. `SELECT Surname FROM Member WHERE SubscriptionPaid = FALSE AND Age >= 18 ORDER BY Surname ASCENDING;` returns adult members who have not paid, in alphabetical order - the two conditions are joined with AND, so both must be true.
The table Sale holds SaleID, ItemName, Quantity, Price and TimeOfSale. `SELECT SUM(Quantity) FROM Sale WHERE ItemName = "Tea";` gives the number of teas sold, while `SELECT AVERAGE(Price) FROM Sale;` gives the mean price of everything sold. Neither returns a list, because each replaces the records with a single number.
Flashcards
Click a card to flip it. Mark honestly — the tracker uses this.
Exam-style questions
8 questions, 28 marks. Write your answer and press Mark my answer — it is marked against the scheme and tells you which points you made and what would gain the next mark. You can still reveal the scheme and mark it yourself instead.
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Exam technique for this topic
- Choose the data type from the data and say why: a rate is real because it has a fractional part; a quantity is integer because it is whole; whether something is in service is Boolean because there are only two answers.
- When you name a primary key, add the reason: the value is unique to each record, so no two records can be confused. Name a field that could not be unique - Model, Name, Date - and you will not get the mark.
- Write SQL with each clause on a new line and the keywords in full: ORDER BY HourlyRate DESCENDING, never ORDER BY HourlyRate DESC. The syllabus spells both words out.
- For an output question, apply WHERE before SELECT and ORDER BY before you write the answer, and show the result as a small table with the selected field names as headings.
- Keep COUNT, SUM and AVERAGE apart by the question's wording: 'how many' is COUNT, 'total' is SUM, 'average' or 'mean' is AVERAGE.
Why students lose marks here
Where this fits in O Level Computer Science 2210
This page is one topic of Cambridge O Level Computer Science 2210. O Level Computer Science 2210 is the course home: every area and every topic in Cambridge's own order, the papers you sit and what each one tests, and the exam-practice tools. The full topic list, with each topic linked to its revision page, is on the O Level Computer Science 2210 syllabus page.