Skip to main content

SQL Databases: Extracting Data Like a Pro

By Prof. David Chen, CS Examiner·Updated April 18, 2026
A snippet of SQL code showing a SELECT and WHERE query executed on a relational database.

What is the exact structure of a 4-line CAIE SQL Query?

Every O-Level SQL query follows this exact pattern. 1) SELECT (the columns you want to output). 2) FROM (the exact name of the table). 3) WHERE (the conditions/filters using operators like >, <, AND, OR). 4) ORDER BY (the column to sort by, plus ASC or DESC). Memorize this 4-step sequence.

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.

💡 Tutor's Tip
Output All Fields: If the question asks to output "all details" or "all records", you do not need to type every column name. Just write 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.

Prof. David Chen📋 From the Desk of Prof. David Chen
Never write the word "EQUALS" in your script. Use the actual = 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?
It defines which specific columns you want to extract and view in the final output.
How does the WHERE clause work?
It filters the table, only returning rows where a specific mathematical or logical condition is TRUE.
What is the difference between ASC and DESC in ORDER BY?
ASC sorts A-Z or smallest-to-largest. DESC sorts Z-A or largest-to-smallest.
What is a Primary Key?
A field holding unique, non-repeatable data (like an ID number) used to identify a specific row in the database.

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.

Related Computer Science Articles