A regular Paper 2 question: data types, primary keys, and reading or completing an SQL query on a table.

1. Tables, records and fields

A single-table database stores data in one table. Each record (row) holds the data about one item; each field (column) holds one piece of data about every item.

Table: STUDENTS (8 records, 6 fields)

StudentIDFirstNameYearHouseScorePaid
S001Ayla10Red72TRUE
S002Ben11Blue85TRUE
S003Cem10Green64FALSE
S004Deniz11Red91TRUE
S005Elif10Blue58TRUE
S006Farid11Green77FALSE
S007Grace10Red83TRUE
S008Hasan11Blue69TRUE

2. Data types

Data typeStoresExample
Text / alphanumericLetters, numbers and symbolsFirstName: “Ayla”; StudentID: “S001”
CharacterA single characterGrade: “A”
BooleanOne of two values: TRUE or FALSEPaid: TRUE
IntegerWhole numbersYear: 10
RealNumbers with a decimal partPrice: 4.99
Date/timeA date and/or timeDateOfBirth: 14/05/2010

Phone numbers and IDs with letters are stored as text, not integer: they can start with 0 and are never used in calculations.

3. Validation

Validation checks that data entered is sensible and reasonable before it is stored.

CheckExample
Range checkYear must be between 7 and 13
Length checkStudentID must be exactly 4 characters
Type checkScore must be an integer
Presence checkFirstName must not be left blank
Format checkStudentID must be one letter followed by three digits
Check digitThe last digit of an ISBN or barcode

4. Primary keys

A primary key is a field that holds a unique value for every record, so each record can be identified. In STUDENTS, StudentID is the primary key. FirstName, Year or House would not work, because two students could share the same value.

5. SQL

Structured query language (SQL) is used to search a database.

KeywordPurposeExample
SELECTWhich fields to show (* = all fields)SELECT FirstName, Score
FROMWhich table to searchFROM STUDENTS
WHEREWhich records to includeWHERE Year = 11
AND / ORCombine conditionsWHERE House = ‘Red’ AND Paid = TRUE
ORDER BY … ASC / DESCSort the results in ascending or descending orderORDER BY Score DESC
SUMAdd up a number fieldSELECT SUM(Score)
COUNTCount the records foundSELECT COUNT(*)

Example: SELECT FirstName, Score FROM STUDENTS WHERE Year = 11 ORDER BY Score DESC; returns Deniz 91, Ben 85, Farid 77, Hasan 69.

Try it: SQL sandbox

Write a query on the STUDENTS table above, or pick an example. Supported: SELECT, FROM, WHERE, AND, OR, ORDER BY … ASC/DESC, SUM, COUNT.

6. Examiner tips

  1. Use the field names exactly as they appear in the table.
  2. Text values go in quotes in SQL; numbers and TRUE/FALSE do not.
  3. Phone numbers are text, not integer, because of leading zeros and spaces.
  4. Give a reason for a primary key: “StudentID is unique for every student”.
  5. Count records and fields carefully; don’t count the header row as a record.
  6. Check AND vs OR: AND needs both conditions to be true; OR needs at least one.
  7. Copy the output exactly in the order the ORDER BY gives.

7. Practice questions

Practice: answer and check instantly

Type a short answer, then press Check or Enter.

Show answer

Date/time.

Show answer

Boolean.

Show answer

Real.

Show answer

StudentID: it is unique for every student.

Show answer

3: Ayla, Deniz and Grace.

Show answer

277 = 72 + 64 + 58 + 83.

Show answer

3: Ben (85), Deniz (91) and Grace (83).

Show answer

ORDER BY (with ASC or DESC).

Exam-style questions

9. Write an SQL query to show the FirstName and Score of all Year 11 students, highest score first. [4]

Show mark guide

SELECT FirstName, Score [1] FROM STUDENTS [1] WHERE Year = 11 [1] ORDER BY Score DESC; [1]

10. State the output of: SELECT StudentID, FirstName FROM STUDENTS WHERE Paid = FALSE OR Score < 60 [3]

Show mark guide

S003 Cem · S005 Elif · S006 Farid (Cem and Farid have not paid; Elif scored 58).

8. Topic test: predict your grade

Topic test: predict your grade

15 questions, about 10 minutes. Answer them all, then press Mark my test.

1. What is a record?
2. What is a field?
3. How many fields does the STUDENTS table have?
4. Which data type for a phone number such as 0212 555 0100?
5. Which data type stores TRUE or FALSE?
6. Which data type stores a single letter grade?
7. What is the purpose of a primary key?
8. Which field would make a good primary key?
9. Which validation check makes sure a field is not left empty?
10. In SQL, which keyword chooses the fields to show?
11. Which keyword says which records to include?
12. ORDER BY Score DESC sorts the scores…
13. What does SELECT COUNT(*) FROM STUDENTS return?
14. WHERE House = ‘Red’ AND Year = 11 returns…
15. Which query adds up all the scores?

Frequently asked questions

What is a primary key?

A primary key is a field that uniquely identifies each record in a database table, such as a StudentID.

How do you write an SQL query for IGCSE?

Use SELECT to choose fields, FROM to name the table, WHERE to filter records and ORDER BY to sort, for example SELECT Name FROM Student WHERE Age greater than 15 ORDER BY Name.

What data types are used in databases?

Common data types are text or alphanumeric, character, Boolean, integer, real and date or time.

Was this page helpful?