2029–2031 edition · for exams from June 2029. Students sitting exams up to November 2028 follow the current course.
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
- 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
- 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:
| Field | Data type | Validation |
|---|---|---|
| StudentID | Text | Presence; format S followed by 3 digits; must be unique |
| FirstName | Text | Presence; length ≤ 30 |
| ClassID | Text | Presence; format |
| Age | Integer | Range 11 to 19 |
| Fees | Currency | Range ≥ 0 |
Data types
| Data type | Holds | Example |
|---|---|---|
| Text | Letters, digits and symbols | "Ayesha Khan", phone "03001234567" |
| Character | A single character | "M", grade "A" |
| Boolean | One of two values | True/False, Yes/No (e.g. FeesPaid) |
| Integer | Whole number | 15 |
| Decimal | Number with a fractional part | 1.75 (height in metres) |
| Date | A calendar date | 04/10/2026 |
| Time | A time of day | 14:30 |
| Currency | An amount of money, shown with the currency symbol and 2 decimal places | 275.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) | Teacher | Room |
|---|---|---|
| 10A | Mr Khan | R12 |
| 10B | Ms Ali | R14 |
STUDENT
| StudentID (PK) | FirstName | ClassID (FK) | Age | Fees |
|---|---|---|---|---|
| S001 | Ayesha | 10A | 15 | 250.00 |
| S002 | Bilal | 10B | 16 | 300.00 |
| S003 | Hina | 10A | 16 | 250.00 |
| S004 | Omar | 10B | 15 | 275.50 |
| S005 | Sara | 10A | 17 | 300.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:
| FirstName | ClassID | Teacher | Room |
|---|---|---|---|
| Ayesha | 10A | Mr Khan | R12 |
| Bilal | 10B | Ms Ali | R14 |
| Hina | 10A | Mr Khan | R12 |
| Omar | 10B | Ms Ali | R14 |
| Sara | 10A | Mr Khan | R12 |
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 withAND(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:
| Faulty | Corrected | Problem |
|---|---|---|
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.ClassID | Join on the foreign key = primary key |
INSERT INTO STUDENT VALUES ('S007', 'Ali', 16); | List the fields, or give a value for every field | Values 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 & SQL
Two-table database and INNER JOIN
SQL for this topic17run them on the ZAK Academy database in the SQL Lab
- SELECT * — every column of a tableThe simplest query. Look at the STUDENT table first, then pick columns.
- Two tables: classes and their teachers, by roomThe 2029 O Level syllabus adds INNER JOIN … ON: link CLASS to TEACHER through the foreign key TeacherID, keep the Computer Science classes and sort them.
- Choose the columns you wantName the fields, separated by commas — the result has only those columns, in that order.
- WHERE with a text valueText is written in single quotes. Only rows where the condition is TRUE are returned.
- AND, OR and bracketsAND binds tighter than OR — use brackets to say what you mean, exactly as in Boolean logic.
- ORDER BY ascendingAlphabetical by last name — ASC is the default.
- ORDER BY descending, two keysHighest salary first; ties broken by last name. LIMIT keeps the top few.
- COUNT(*) — how many rowsHow many students are in form 10A? COUNT(*) counts matching rows.
- SUM — total of a columnTotal monthly salary bill for the Computer Science department.
- INSERT INTO — add a rowValues in the same order as the columns you list. Then SELECT to prove it is there.
- INSERT INTO, then check it with SELECTAdd a book with every field in the order the table defines, then select it back. Text in single quotes, numbers without.
- COUNT across two tables with AND / ORHow many of Zafar Khan's classes run on Monday or Wednesday? The brackets keep the OR together before the AND.
- SUM across two tablesTotal fee of the subjects taught on Monday: SUM over a joined, filtered set.
- Correct the SQL statement (meant to fail)A query with two mistakes for you to correct, as in 2029 objective 11.2.2: the FROM is missing and the text value has no quotes. Run it, read the message, then fix it.
- 2210: names and marks of students who scored over 70 in CSO-A, highest firstSingle-table query with WHERE, AND and ORDER BY — the classic 2210 Paper 2 SQL question.
- 2210: how many books were published after 2015?COUNT with a WHERE — the 2210 syllabus expects SUM and COUNT.
- 2210: total copies of books by an authorSUM over a filtered set.
Key terms13use these exact words in the exam
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.
- 9. Databases2026–2028 topic · 109 past-paper questions

