A Global Language for Organised Data
Every country on earth keeps records. Tax authorities in Stockholm, hospital registries in Nairobi, shipping manifests in Singapore - all of them rely on structured collections of data that can be searched, sorted, and updated reliably. The tool that makes this possible is the database, and the language used to query most of the world's databases is SQL. Whether you are studying in London, Lagos, or Lima, the Cambridge IGCSE Computer Science (0478) syllabus expects you to understand how databases are designed, how data types are chosen, what makes a good primary key, and how to write queries that retrieve exactly the information you need.
This topic bridges the gap between abstract data storage and the real systems that power online banking, airline booking, school administration, and e-commerce across every continent. The concepts here are not theoretical curiosities. They are the practical foundation of how modern organisations manage information.
Database Fundamentals
A database is an organised collection of structured data, stored electronically so that it can be easily accessed, managed, and updated. At the IGCSE level, you will work primarily with single-table flat-file databases, though you should understand that real-world systems often use multiple linked tables (relational databases).
Key Terminology
Three terms form the vocabulary of every database discussion:
- Table: The structure that holds all the data. Think of it as a grid - rows and columns - where each column represents a category of information, and each row represents one complete entry.
- Record: A single row in the table. Each record holds all the data about one entity (one student, one product, one transaction).
- Field: A single column in the table. Each field holds one category of data across all records (for example, "Surname" or "DateOfBirth").
Consider a school database tracking student enrolment. The table might look like this:
| StudentID | FirstName | Surname | DateOfBirth | Year Group |
|---|---|---|---|---|
| S001 | Amara | Okafor | 2011-03-14 | 10 |
| S002 | Liam | Chen | 2010-08-22 | 11 |
| S003 | Sofia | Petrov | 2011-01-05 | 10 |
Here, each row is a record (one student), and each column is a field (StudentID, FirstName, Surname, DateOfBirth, Year Group). The whole structure is the table.
Designing a Single-Table Database
When an exam question asks you to design a database table from a written description, follow these steps:
- Identify the entities being stored (books, patients, orders).
- List the fields needed for each entity - what information must be recorded?
- Choose an appropriate data type for each field.
- Select a primary key - a field (or combination) that uniquely identifies each record.
Data Types
Every field in a database must be assigned a data type that determines what kind of values it can store and what operations can be performed on it. Choosing the correct data type is a skill that Cambridge examiners test directly. A telephone number stored as an INTEGER would lose its leading zero. A price stored as TEXT could not be used in a SUM calculation. Precision here matters.
| Data Type | Description | Example Values | When to Use |
|---|---|---|---|
| TEXT / STRING | Any sequence of characters (letters, digits, symbols) | "Amara", "42 High Street", "AB12 3CD" | Names, addresses, descriptions, postcodes, phone numbers |
| CHAR(n) | A fixed-length string of exactly n characters | "M", "F", "GB" | Gender codes, country codes, any field where the length never varies |
| INTEGER | A whole number (no decimal places) | 17, 250, -3 | Quantities, ages, year groups, number of items in stock |
| REAL | A number with a decimal/fractional part | 9.99, 36.5, -0.75 | Prices, weights, temperatures, measurements |
| BOOLEAN | One of two possible values: TRUE or FALSE | TRUE, FALSE | Yes/no fields: "IsMember", "HasPaid", "InStock" |
| DATE / TIME | A calendar date or clock time | 2025-09-01, 14:30:00 | Dates of birth, appointment times, order dates |
Primary Keys
A primary key is a field (or combination of fields) that uniquely identifies each record in a table. No two records can share the same primary key value, and the primary key field cannot be left empty (null).
Why Primary Keys Matter
Without a primary key, the database has no reliable way to distinguish one record from another. Imagine a school table where two students share the name "Maria Garcia." If the table has no unique identifier, searching for Maria Garcia would return both records with no way to tell which is which. The primary key solves this by guaranteeing that every record has a distinct identity.
Primary keys also enable fast retrieval. The database engine builds an index on the primary key, allowing it to locate a specific record without scanning every row in the table.
Natural vs. Artificial Keys
Sometimes a field in the data is naturally unique. An ISBN uniquely identifies a book. A national insurance number uniquely identifies a person in the UK. A passport number uniquely identifies a passport. These are natural keys.
Often, though, no single field in the data is guaranteed to be unique. Names repeat. Dates repeat. Addresses can be shared. In these cases, you create an artificial key - a field whose sole purpose is to provide a unique identifier. StudentID, ProductCode, OrderNumber: these are invented specifically to serve as primary keys.
SQL: Structured Query Language
SQL is the standard language for interacting with databases. From small classroom exercises to vast commercial systems used by companies in Tokyo, Berlin, and Sao Paulo, SQL is the shared vocabulary. The IGCSE syllabus focuses on retrieval queries - statements that extract data from existing tables.
The SELECT Statement
The basic structure of an SQL query is:
SELECT field1, field2 FROM TableName WHERE condition ORDER BY field ASC;
Each clause plays a specific role:
- SELECT - specifies which fields to display. Use
*to select all fields. - FROM - identifies the table to query.
- WHERE - filters the records returned based on one or more conditions. Optional.
- ORDER BY - sorts the results by a specified field, either ASC (ascending, the default) or DESC (descending). Optional.
Comparison and Logical Operators
| Operator | Meaning | Example |
|---|---|---|
| = | Equal to | WHERE YearGroup = 10 |
| < | Less than | WHERE Price < 20.00 |
| > | Greater than | WHERE Stock > 0 |
| <= | Less than or equal to | WHERE Age <= 16 |
| >= | Greater than or equal to | WHERE Score >= 50 |
| <> | Not equal to | WHERE Country <> 'UK' |
| LIKE | Pattern matching (use % as wildcard) | WHERE Surname LIKE 'S%' |
| AND | Both conditions must be true | WHERE Age > 14 AND Age < 18 |
| OR | At least one condition must be true | WHERE Country = 'France' OR Country = 'Spain' |
| NOT | Reverses the condition | WHERE NOT HasPaid = TRUE |
Aggregate Functions
SQL provides functions that perform calculations across multiple records:
- COUNT(*) - returns the number of records matching the query.
- SUM(field) - returns the total of all values in a numeric field.
- AVG(field) - returns the arithmetic mean of all values in a numeric field.
These are often combined with WHERE to narrow the calculation. For instance, SELECT AVG(Score) FROM Results WHERE Subject = 'Physics' returns the average physics score.
Worked SQL Examples
The examples below use this sample table, Products:
| ProductID | ProductName | Category | Price | InStock |
|---|---|---|---|---|
| P001 | Wireless Mouse | Accessories | 12.99 | TRUE |
| P002 | USB Keyboard | Accessories | 18.50 | TRUE |
| P003 | Monitor Stand | Furniture | 34.00 | FALSE |
| P004 | Webcam HD | Accessories | 29.99 | TRUE |
| P005 | Desk Lamp | Furniture | 22.75 | TRUE |
Example 1: Show the names and prices of all accessories, sorted by price from lowest to highest.
SELECT ProductName, Price FROM Products WHERE Category = 'Accessories' ORDER BY Price ASC;
| ProductName | Price |
|---|---|
| Wireless Mouse | 12.99 |
| USB Keyboard | 18.50 |
| Webcam HD | 29.99 |
Example 2: Count the number of products that are currently in stock.
SELECT COUNT(*) FROM Products WHERE InStock = TRUE;
Result: 4
Example 3: Show all products costing more than 15.00 that are in stock, sorted by price from highest to lowest.
SELECT * FROM Products WHERE Price > 15.00 AND InStock = TRUE ORDER BY Price DESC;
| ProductID | ProductName | Category | Price | InStock |
|---|---|---|---|---|
| P004 | Webcam HD | Accessories | 29.99 | TRUE |
| P005 | Desk Lamp | Furniture | 22.75 | TRUE |
| P002 | USB Keyboard | Accessories | 18.50 | TRUE |
Example 4: Calculate the average price of all products.
SELECT AVG(Price) FROM Products;
Result: 23.646
Example 5: Find all products whose name starts with "W".
SELECT * FROM Products WHERE ProductName LIKE 'W%';
| ProductID | ProductName | Category | Price | InStock |
|---|---|---|---|---|
| P001 | Wireless Mouse | Accessories | 12.99 | TRUE |
| P004 | Webcam HD | Accessories | 29.99 | TRUE |
Worked Exam-Style Questions
A veterinary clinic wants to create a database to store information about the animals it treats. For each animal, the clinic needs to record: the animal's name, species (e.g. dog, cat, rabbit), date of birth, weight in kilograms, whether the animal has been vaccinated, and the owner's telephone number.
(a) Design a suitable database table. For each field, give a field name, a suitable data type, and identify the primary key. [5 marks]
(b) Explain why the animal's name would not be a suitable primary key. [1 mark]
Model answer (a):
| Field Name | Data Type | Primary Key? |
|---|---|---|
| AnimalID | TEXT | Yes |
| AnimalName | TEXT | No |
| Species | TEXT | No |
| DateOfBirth | DATE | No |
| Weight | REAL | No |
| Vaccinated | BOOLEAN | No |
| OwnerPhone | TEXT | No |
Note that AnimalID is an artificial primary key created because none of the given fields is guaranteed to be unique. OwnerPhone is stored as TEXT to preserve any leading zeros and because no arithmetic is performed on it.
Model answer (b): The animal's name would not be a suitable primary key because multiple animals could have the same name (two dogs both called "Max"), so it cannot uniquely identify each record.
A school stores student exam results in a table called Results with the following fields: StudentID (TEXT), StudentName (TEXT), Subject (TEXT), Score (INTEGER), Grade (CHAR).
Write SQL queries to:
(a) Display the names and scores of all students who scored 70 or above in Mathematics, sorted by score from highest to lowest. [3 marks]
(b) Count the number of students who achieved a grade of 'A'. [2 marks]
(c) Display all fields for students who scored less than 40 in either English or Science. [3 marks]
Model answer (a):
SELECT StudentName, Score FROM Results WHERE Subject = 'Mathematics' AND Score >= 70 ORDER BY Score DESC;
Model answer (b):
SELECT COUNT(*) FROM Results WHERE Grade = 'A';
Model answer (c):
SELECT * FROM Results WHERE Score < 40 AND (Subject = 'English' OR Subject = 'Science');
Common Mistakes
- Choosing INTEGER for phone numbers or postcodes. These values contain leading zeros or letters that INTEGER cannot preserve. Always use TEXT for identifiers that are not used in arithmetic.
- Using a non-unique field as a primary key. A surname, a date of birth, or a city name can repeat across records. If no naturally unique field exists, create an artificial key.
- Forgetting ORDER BY defaults to ASC. If a question asks for results "in ascending order," ASC is technically optional but good practice to state. If the question says "highest first" or "largest to smallest," you must write DESC.
- Confusing COUNT with SUM. COUNT returns how many records match; SUM adds up the values in a numeric field. "How many students scored above 80" needs COUNT. "What is the total of all scores" needs SUM.
- Omitting quotes around text values in SQL. String comparisons require single quotes:
WHERE Subject = 'Mathematics'. Numeric comparisons do not:WHERE Score >= 70. Mixing these up causes syntax errors. - Ignoring brackets with AND/OR. SQL evaluates AND before OR. The query
WHERE Score < 40 AND Subject = 'English' OR Subject = 'Science'does not do what most students intend. Use brackets to group the OR:WHERE Score < 40 AND (Subject = 'English' OR Subject = 'Science').
Self-Check Questions
- A bookshop stores data about its books. Each record includes the ISBN, title, author, price, and number of copies in stock. Which field should be the primary key? Justify your answer.
- A field stores whether a library book is currently on loan. What data type should this field be, and why?
- Write an SQL query to display the titles and authors of all books from the table Books where the price is less than 10.00, sorted alphabetically by title.
- Explain the difference between
SELECT COUNT(*) FROM Orders WHERE Status = 'Shipped'andSELECT SUM(Quantity) FROM Orders WHERE Status = 'Shipped'. - A database stores employee records. The fields are: EmployeeID, Name, Department, Salary, StartDate. Write an SQL query to find the average salary of employees in the "Engineering" department.
1. ISBN should be the primary key because each book has a unique ISBN, so it uniquely identifies every record. Fields like title or author can repeat (different editions, same author writing multiple books).
2. BOOLEAN, because the field has exactly two possible states: on loan (TRUE) or not on loan (FALSE). No other data type fits a simple yes/no value as precisely.
3.
SELECT Title, Author FROM Books WHERE Price < 10.00 ORDER BY Title ASC;4. The COUNT query returns the number of orders with a status of 'Shipped' (how many shipped orders exist). The SUM query adds up the Quantity values across all shipped orders (the total number of items shipped). COUNT counts records; SUM totals a numeric field.
5.
SELECT AVG(Salary) FROM Employees WHERE Department = 'Engineering';A thorough set of revision notes on the Databases topic for Cambridge IGCSE Computer Science (0478), covering database fundamentals, data types, primary keys, and SQL queries with worked examples. The article includes exam-style questions with model answers, a common mistakes guide, and self-check exercises to consolidate understanding.
Àsìkò méjì (Comment(s))