SQL Databases: Extracting Data Like a Pro

What is the exact structure of a 4-line CAIE SQL Query?
Table of Contents
SQL (Structured Query Language) is the industry standard for interacting with databases. In Paper 2, you must write completely accurate SQL scripts. If you miss a quotation mark around a string, you lose the mark. This guide from our Ultimate O-Level Computer Science Guide provides the exact syntax rules required by examiners.
1. The Database Vocabulary Trap
Before writing SQL, you must know what you are looking at. A relational database is a table of data, but CAIE uses very specific terminology.
- Table / Relation: The entire grid of data.
- Field / Column: A vertical column holding a specific TYPE of data (e.g., 'FirstName', 'Price').
- Record / Row: A horizontal line holding all the data relating to ONE specific item or person (e.g., all data regarding Student ID 142).
- Primary Key: A specific Field (column) where every entry is guaranteed to be 100% unique (e.g., a Passport Number or Barcode). It prevents duplicate data.
2. The 4-Step Query Builder
Assume we have a table called STUDENTS. It has fields: StudentID, Name, Age, and PassedExam.
The Question: "Write an SQL query to output the Names and Ages of all students who passed the exam, sorting the list from oldest to youngest."
SELECT Name, Age
-- We only ask for Name and Age. We do not ask for StudentID.
FROM STUDENTS
-- The exact name of the table.
WHERE PassedExam = TRUE
-- The filter condition.
ORDER BY Age DESC;
-- We want oldest first, so we sort the Age column by DESCending order.
SELECT *. The asterisk is the wildcard operator meaning 'everything'.3. Mastering the WHERE Clause (Strings & Booleans)
The WHERE clause is where students lose the most marks. You must use the correct data types.
Rule 1: Text requires Quotes
If you are searching a string/text field, you MUST wrap the search term in single or double quotes. If you write WHERE City = London, it is a syntax error. It must be WHERE City = "London".
Rule 2: Numbers and Booleans do NOT
If you are filtering an Integer or Boolean, do not use quotes. E.g., WHERE Age > 18 or WHERE HasPaid = FALSE.
Rule 3: Using AND / OR
You can stack filters indefinitely. Be very careful with logic. To find expensive blue cars only, you write:WHERE Color = "Blue" AND Price > 10000.
= sign. The comparison operators allowed are: = (Equal), > (Greater than), < (Less than), >=, <=, and <> (Not Equal).Frequently Asked Questions
What does the SELECT keyword do in SQL?▼
How does the WHERE clause work?▼
What is the difference between ASC and DESC in ORDER BY?▼
What is a Primary Key?▼
Stop Guessing, Start Scoring
Get instant access to 500+ CAIE-aligned practice questions, worked solutions, and AI-powered mock exams across all O-Level subjects.