Skip to content

2029–2031 edition · for exams from June 2029. Students sitting exams up to November 2028 follow the current course.

2210 · 04782029–2031 editionPaper 2 · Algorithms and Programming§11.1, §11.2

11. Databases and Structured Query Language (SQL)

Single- and two-table relational databases with fields, records, data types, primary and foreign keys, and SQL to retrieve, insert and join data.

What you need to know211 learning objectives, as printed in the syllabus

  1. 11.1DatabasesChanged2026–2028 syllabus: Topic 9

    Grows to two-table databases with foreign keys and joins; adds decimal, time and currency data types.

    Learning objectives (8)
    • 11.1.1Define a single-table database from given data storage requirements, including: (a) fields; (b) records; (c) validations
    • 11.1.2Define a two-table relational database from given data storage requirements, including: (a) fields; (b) records; (c) validations
    • 11.1.3Identify data types, limited to: (a) text; (b) character; (c) Boolean; (d) integer; (e) decimal; (f) date; (g) time; (h) currency
    • 11.1.4Explain the purpose of a primary key
    • 11.1.5Identify a suitable primary key for a given table
    • 11.1.6Explain the purpose of a foreign key to establish a link and join two tables
    • 11.1.7Identify a suitable foreign key for a given database
    • 11.1.8Demonstrate how two tables can be joined in a database
  2. 11.2SQLChanged2026–2028 syllabus: Topic 9

    Adds INSERT INTO, INNER JOIN … ON, correcting SQL statements and queries on two tables.

    Learning objectives (3)
    • 11.2.1Write structured query language (SQL) statements using one or more conditions to retrieve data from a one and two table database
    • 11.2.2Correct SQL statements
    • 11.2.3Explain and demonstrate the result for a given SQL statement, limited to: (a) SELECT, FROM, WHERE; (b) INSERT INTO; (c) ORDER BY DESCENDING; (d) ORDER BY ASCENDING; (e) SUM; (f) COUNT; (g) AND; (h) OR; (i) INNER JOIN, ON

Objectives quoted from the 2029–2031 syllabus, Version 1, September 2026; © Cambridge University Press & Assessment.

Notes2every learning objective explained, with worked examples

11.1Databases

A database stores data in tables of records and fields. This edition goes beyond one table: you must design single- and two-table relational databases, pick data types, choose primary and foreign keys and show how two tables are joined.

Tables, records and fields

  • A table holds data about one kind of thing (students, books).
  • A record is one row — all the data about one item.
  • A field is one column — one piece of data about every item (e.g. FirstName).

Defining a single-table database from requirements means listing each field, its data type, a sample record, and the validation for each field:

FieldData typeValidation
StudentIDTextPresence; format S followed by 3 digits; must be unique
FirstNameTextPresence; length ≤ 30
ClassIDTextPresence; format
AgeIntegerRange 11 to 19
FeesCurrencyRange ≥ 0

Data types

Data typeHoldsExample
TextLetters, digits and symbols"Ayesha Khan", phone "03001234567"
CharacterA single character"M", grade "A"
BooleanOne of two valuesTrue/False, Yes/No (e.g. FeesPaid)
IntegerWhole number15
DecimalNumber with a fractional part1.75 (height in metres)
DateA calendar date04/10/2026
TimeA time of day14:30
CurrencyAn amount of money, shown with the currency symbol and 2 decimal places275.50

Phone numbers and IDs with leading zeros are text — you never do arithmetic on them.

Primary keys

A primary key is a field that uniquely identifies each record in a table — no two records can have the same value, and it cannot be empty.

Choosing one: pick a field whose values can never repeat (StudentID, ISBN, OrderNumber). Names, dates of birth and towns are not suitable because two records could share them. If no field is unique, add an ID field.

Two-table relational databases and foreign keys

A relational database stores data in linked tables, so data such as a class's teacher is stored once instead of being repeated in every student record (less duplication and fewer inconsistencies).

A foreign key is a field in one table that is the primary key of another table. It establishes the link so the two tables can be joined.

CLASS

ClassID (PK)TeacherRoom
10AMr KhanR12
10BMs AliR14

STUDENT

StudentID (PK)FirstNameClassID (FK)AgeFees
S001Ayesha10A15250.00
S002Bilal10B16300.00
S003Hina10A16250.00
S004Omar10B15275.50
S005Sara10A17300.00

ClassID is the primary key of CLASS and a foreign key in STUDENT. One class has many students (a one-to-many link). Validation on the foreign key: its value must already exist in CLASS.

Joining two tables

To join the tables, match each record in STUDENT to the record in CLASS whose ClassID is the same:

FirstNameClassIDTeacherRoom
Ayesha10AMr KhanR12
Bilal10BMs AliR14
Hina10AMr KhanR12
Omar10BMs AliR14
Sara10AMr KhanR12

In SQL this is INNER JOIN … ON STUDENT.ClassID = CLASS.ClassID (see §11.2). Only records with a matching value in both tables appear.

Exam tips

  • Primary key: 'uniquely identifies each record'. Foreign key: 'a primary key from another table, used to link the tables'.
  • When choosing a key, justify it: 'StudentID because no two students share it'.
  • When defining a table, give every field a data type and a sensible validation check.
  • Pick data types carefully: currency for money, Boolean for yes/no, text for phone numbers.
  • Use 'record' for a row and 'field' for a column — examiners mark the words.

Mistakes that lose marks

  • Choosing a name or date of birth as a primary key.
  • Putting the foreign key in the wrong table (it goes on the 'many' side — STUDENT holds ClassID).
  • Storing phone numbers as integer (leading zeros are lost).
  • Mixing up records and fields.
  • Saying a foreign key must be unique in its own table — it can repeat.

11.2SQL

SQL (Structured Query Language) is how you ask a database for data and add to it. You must write queries on one or two tables, correct faulty SQL, and say exactly what a given statement outputs. The examples use the CLASS and STUDENT tables from §11.1.

SELECT, FROM, WHERE with AND / OR

  • SELECT — the fields to show (* = all fields).
  • FROM — the table.
  • WHERE — the condition records must meet; combine conditions with AND (both true) / OR (either true). Text values go in quotes; numbers do not.
SELECT FirstName, Age
FROM STUDENT
WHERE Age > 15 AND ClassID = '10A';

Result: Hina 16 and Sara 17 (Ayesha is 15, so not > 15; Bilal is in 10B).

ORDER BY ascending and descending

ORDER BY field ASC sorts A→Z / smallest first (the default); DESC sorts Z→A / largest first. It comes after WHERE.

SELECT FirstName, Fees
FROM STUDENT
ORDER BY Fees DESC;

Result order: the three 300.00 students, then Omar 275.50, then the two 250.00 students. Add a second field to break ties: ORDER BY Fees DESC, FirstName ASC.

SUM and COUNT

SUM(field) adds the values in a numeric field; COUNT(field) or COUNT(*) counts the matching records. Each returns one value.

SELECT SUM(Fees)
FROM STUDENT
WHERE ClassID = '10A';

Result: 800.00 (250.00 + 250.00 + 300.00).

SELECT COUNT(StudentID)
FROM STUDENT
WHERE ClassID = '10B' OR Age = 17;

Result: 3 (Bilal and Omar are in 10B; Sara is 17).

INSERT INTO

Adds a new record. List the fields, then the values in the same order.

INSERT INTO STUDENT (StudentID, FirstName, ClassID, Age, Fees)
VALUES ('S006', 'Zara', '10B', 15, 300.00);

After this, SELECT COUNT(*) FROM STUDENT; gives 6. If values are given for every field in table order, the field list may be left out.

INNER JOIN … ON (two tables)

INNER JOIN combines records from two tables where the ON condition matches — normally the foreign key = primary key. Write fields as Table.Field when both tables are involved.

SELECT STUDENT.FirstName, CLASS.Teacher
FROM STUDENT
INNER JOIN CLASS ON STUDENT.ClassID = CLASS.ClassID
WHERE CLASS.Room = 'R14'
ORDER BY STUDENT.FirstName ASC;

Result: Bilal, Ms Ali and Omar, Ms Ali — the students taught in room R14, sorted by name.

Order of clauses: SELECT … FROM … INNER JOIN … ON … WHERE … ORDER BY ….

Correcting SQL statements

Common faults to look for:

FaultyCorrectedProblem
SELECT FirstName STUDENT WHERE Age = 16;SELECT FirstName FROM STUDENT WHERE Age = 16;Missing FROM
SELECT FirstName, Age FROM STUDENT WHERE ClassID = 10A;… WHERE ClassID = '10A';Text value needs quotes
SELECT FirstName FROM STUDENT ORDER BY Age WHERE Age > 15;… WHERE Age > 15 ORDER BY Age;WHERE must come before ORDER BY
SELECT FirstName FROM STUDENT WHERE ClassID = '10A' OR '10B';… WHERE ClassID = '10A' OR ClassID = '10B';Each condition needs the field
SELECT COUNT(Fees) FROM STUDENT WHERE ClassID = '10A'; (to total the fees)SELECT SUM(Fees) …COUNT counts records; SUM adds values
… INNER JOIN CLASS ON STUDENT.FirstName = CLASS.ClassID… ON STUDENT.ClassID = CLASS.ClassIDJoin on the foreign key = primary key
INSERT INTO STUDENT VALUES ('S007', 'Ali', 16);List the fields, or give a value for every fieldValues must match the fields

Exam tips

  • Write SQL keywords in capitals and spell field and table names exactly as given in the question.
  • Put quotes round text values (and dates if the question does), never round numbers.
  • Clause order earns marks: SELECT … FROM … INNER JOIN … ON … WHERE … ORDER BY.
  • When giving the result of a query, show only the fields selected, in the order selected, and in the ORDER BY order.
  • For a join, the ON condition is the foreign key equal to the primary key — name both tables.
  • Use AND when both conditions must be true; use OR when either may be — and repeat the field name each time.

Mistakes that lose marks

  • Using COUNT when the question asks for a total (SUM), or the other way round.
  • Writing WHERE ClassID = '10A' OR '10B' instead of repeating the field.
  • Forgetting FROM, or putting ORDER BY before WHERE.
  • Including fields in the result that were not in the SELECT list.
  • Writing DESCENDING in full in code — the keyword is DESC (and ASC).

Infographics2download any diagram as PNG or SVG

Single-table database & SQLField = column (one item of data). Record = row (one entity). Primary key = the field that uniquelyidentifies each record.STUDENTStudentIDNameFormMarkS001Ali10A82S002Hina10B64S003Omar10A91S004Sara10B70StudentID = primary key (unique, never blank).Data types: text, character, Boolean, integer, real, date/time.SELECT Name, MarkFROM STUDENTWHERE Mark >= 70ORDER BY Mark DESC;NameMarkOmar91Ali82Sara70result: 3 records,2 fields, sortedhighest firstAggregatesSELECT COUNT(*) FROM STUDENT WHERE Form = "10A"; → 2SELECT SUM(Mark) FROM STUDENT; → 307ClauseMeaningSELECTwhich fields to show (* = all)FROMwhich tableWHEREcondition: = < > <= >= <> AND ORORDER BYsort: ASC (default) or DESCValidation on fields: range, type, length,format, presence, check digit.Strings in quotes; the semicolon ends the statement.Field names in SELECT are separated by commas.cswithzak.com

Single-table database & SQL

O LevelAS
Two-table database, keys and INNER JOINA foreign key is a field in one table that is the primary key of another table. It links the tables so aquery can join them.CLASSClassID (PK)TeacherRoom10AMs KhanR110BMr AliR2STUDENTStudentID (PK)NameClassID (FK)S1Ayesha10BS2Bilal10AS3Hina10BFK → PKSELECT STUDENT.Name, CLASS.TeacherFROM STUDENTINNER JOIN CLASS ON STUDENT.ClassID = CLASS.ClassIDWHERE CLASS.Room = 'R2'ORDER BY STUDENT.Name;ResultNameTeacherAyeshaMr AliHinaMr AliINSERT INTO STUDENT (StudentID, Name, ClassID)VALUES ('S4', 'Omar', '10A');INNER JOIN keeps only rows thatmatch in both tables.SUM(field), COUNT(*) total or count.Name the table before the field (STUDENT.Name) when both tables have a field of that name.cswithzak.com

Two-table database and INNER JOIN

O Level

SQL for this topic17run them on the ZAK Academy database in the SQL Lab

Key terms13use these exact words in the exam

fieldrecordprimary keyforeign keyrelational databasedata typeSELECTWHEREORDER BYINSERT INTOINNER JOINSUMCOUNT

Test yourself

Check you know the 2029–2031 content

Written for the new syllabus only: every card and question traces to a learning objective above. Rounds are random, and marks earn XP on your dashboard.

3 decks · 34 cards · 16 quiz questions.

From the current course

Most of this topic is taught in the 2026–2028 course today. Its notes and past-paper questions still help — skip anything the 2029–2031 syllabus removed (see the notes above), and remember those past papers answer in pseudocode: write Python instead.

Enroll nowOnline classes