S
Topic 9 Databases · 9.3 · 9.4 · 9.5

Querying Lab

Ask the database questions. SELECT, FROM, WHERE, ORDER BY — the four clauses that carry every Cambridge database question from 2022 to today.

🧪 Exam Mode ON — recall from memory, then reveal

📚 Topic overview

In Lab A you learned how a database is shaped. In Lab B you learn how to ask it questions. Every SQL query you write from now on is one sentence in three parts: SELECT picks the columns you want, FROM names the table, WHERE filters down to only the records that match your condition, and ORDER BY sorts the results the way you want to see them.

The reason we're teaching these three lessons together (not one at a time) is that Cambridge always tests them together. Every single Paper 2 database question from 2022 to 2026 combines at least two of these clauses in one query — often all three. Learning them in isolation is how students lose exam marks. Learning them as one sentence is how you keep them.

Before you start: you need the vocabulary from Lab A — fields, records, data types, primary keys. If any of those feel shaky, jump back for five minutes.

🎯 Learning objectives

9.3 SELECT ... FROM
9.3.a Read and complete SQL scripts that use SELECT ... FROM to return one or more fields from a table.
9.3.b Understand that fields listed in SELECT are returned in the order given, separated by commas.
9.3.c Recognise that SQL is case-sensitive and spelling-sensitive — field names and table names must match exactly.
9.4 SELECT ... FROM ... WHERE
9.4.a Read and complete SQL scripts that use SELECT ... FROM ... WHERE to return only records that meet a condition.
9.4.b Apply the six logical (comparison) operators: =, <>, <, <=, >, >=.
9.4.c Apply the Boolean operators AND and OR to combine multiple conditions.
9.4.d Apply the correct quoting rules — Text and Date values in quotes, Integer/Real/Boolean values without.
9.4.e Understand that fields used in WHERE do not have to appear in SELECT.
9.5 ORDER BY
9.5.a Read and complete SQL scripts that use ORDER BY to sort results.
9.5.b Distinguish ascending (ASC, the default) from descending (DESC).
9.5.c Place ORDER BY correctly — it always comes after WHERE, at the end of the statement.

📖 Key terminology

TermMeaning
QueryA single SQL request made to a database to return specific data.
SELECTThe SQL keyword that specifies which fields (columns) to return.
FROMThe SQL keyword that specifies which table to query.
WHEREThe SQL keyword that specifies a condition — only records matching the condition are returned.
ORDER BYThe SQL keyword that sorts the returned records.
ASCSort order keyword: ascending (smallest to largest, A to Z). This is the default if you don't say.
DESCSort order keyword: descending (largest to smallest, Z to A). Must be written explicitly.
Logical operatorA symbol that performs a comparison and returns True or False: =, <>, <, <=, >, >=.
Boolean operatorA word that joins multiple conditions: AND, OR.
ConditionThe comparison used in a WHERE clause — always a field, an operator, and a value (e.g. Cost < 3.99).
; (semi-colon)The character that ends an SQL statement. Cambridge past papers always show it.

SQL keywords are conventionally written in UPPERCASE (SELECT, FROM, WHERE). Cambridge mark schemes accept lowercase too, but UPPERCASE is the exam-safe habit — it visually separates keywords from field names.

🧠 Core theory

1. The anatomy of a SELECT query

Every query is the same skeleton:

SELECT   <fields>
FROM     <table>
WHERE    <condition>
ORDER BY <field> [ASC | DESC];
  • Lines 1–2 are required in every query.
  • Lines 3 and 4 are optional — but if both are used, WHERE must come before ORDER BY.
  • The statement ends with ;.

Rule of thumb: if a question tells you to "list", "display", or "output" records, the query starts with SELECT.

2. SELECT — the field order matters

Fields listed in SELECT are returned in the order given, separated by commas. Same data, different query = different output columns.

Query A

SELECT BookName, Author
FROM  Books;

Output:
Picking daisies   J. Frank
Night stars   K. Mars

Query B

SELECT Author, BookName
FROM  Books;

Output:
J. Frank   Picking daisies
K. Mars   Night stars

The order in the query determines the order in the answer.

  • Commas separate fields inside SELECT — never after the last field, never between FROM and the table name.
  • To return every field, most databases support SELECT * — but the Cambridge syllabus does not teach *. Always list the fields explicitly.

3. SQL is case-sensitive and spelling-sensitive

Straight from the textbook: "The case, spellings and order are all important. If the field name in the table is Genre then putting genre in the query is incorrect."

  • Field name is Genre in the table → your query must say Genre exactly.
  • Table name is BookList → your query must say BookList exactly.
  • Misspellings cost marks. Cambridge mark schemes are strict on this.

One exception: SQL keywords (SELECT, FROM, WHERE) are accepted in any case, but write them UPPERCASE anyway for clarity.

4. WHERE — the six logical operators

Present as a single reference. This is the workhorse of exam questions.

OperatorMeaningExample
=Equal toWHERE Fiction = TRUE
<>Not equal toWHERE Cost <> 4.00
<Less thanWHERE Cost < 3.99
<=Less than or equal toWHERE Cost <= 10
>Greater thanWHERE Quantity > 0
>=Greater than or equal toWHERE Quantity >= 100

Anchor point: these are the same comparison operators you met in Chapter 8 (Programming). Same symbols, same meaning. The only difference is that SQL doesn't use != — the "not equal to" operator in SQL is <>.

5. Quoting rules — the make-or-break rule of SQL

This subsection is worth more marks per exam than any other single rule in Topic 9. The evidence base is overwhelming: 2023, 2024 s24, 2024 w24, 2025 w25 examiner reports all flag it.

Data type of valueQuotes in SQL?Example
TextYes — double or singleWHERE Country = "China" or WHERE Country = 'China'
CharacterYesWHERE Grade = 'A'
IntegerNoWHERE Population > 1000000
RealNoWHERE Cost <= 3.99
BooleanNo — this is the trapWHERE Capital = TRUE
Date/TimeYes (treated like text)WHERE Date = "11/12/2019"

Cambridge mark schemes accept both single and double quotes on strings — pick one and be consistent. Never put quotes around a Boolean or a number.

🗝️ One-line rule to remember

"If it's text-like, quote it. If it's number-like or True/False, don't."

6. Boolean operators — combining conditions

You can filter on more than one condition by joining conditions with AND or OR.

OperatorEffect
ANDOnly records that meet both conditions are returned.
ORRecords that meet either condition are returned.

Syllabus note: the Cambridge specification lists AND and OR only. NOT is not required for this syllabus — leave it aside.

7. ORDER BY — sorting the results

  • ORDER BY <field> sorts on that field.
  • The default sort is ascending — you do not need to write ASC, but it's not wrong to.
  • Descending must be written explicitly: ORDER BY <field> DESC.
  • ORDER BY always comes after WHERE, at the end of the statement.

Ascending on numbers = smallest first. Descending on numbers = largest first. Ascending on text = A first, Z last. Descending on text = Z first, A last.

The 2025 s25 MajorCity trap: if the question asks for cities in South America sorted by population, the WHERE filters the continent AND the ORDER BY sorts what's left. A common wrong answer put cities in the wrong order — students filtered correctly but sorted by the wrong field or in the wrong direction.

✏️ Worked examples

All eight examples use the MajorCity table from Cambridge 2025 s25. Same data across every example — the query is what changes.

The MajorCity reference table

Code   City         Capital  Country          Continent       Population
ASY6   Abu Dhabi    TRUE     UAE              Asia            1,566,999
EUY3   Amsterdam    TRUE     Netherlands      Europe          1,174,025
ASY1   Beijing      TRUE     China            Asia           21,766,214
SAY1   Buenos Aires TRUE     Argentina        South America  15,490,415
AFY1   Cairo        TRUE     Egypt            Africa         22,183,200
EUN1   Frankfurt    FALSE    Germany          Europe            796,437
ASY3   Jakarta      TRUE     Indonesia        Asia           11,248,839

Example 1 · Single field, whole table

SELECT City
FROM MajorCity;

Returns: every city name, in the order they appear in the table (no sort applied).

Teaching point: no WHERE = all records. No ORDER BY = original order.

Example 2 · Multiple fields — order matters

SELECT City, Population
FROM MajorCity;

SELECT Population, City
FROM MajorCity;

Same data, different column order. Students who reverse the fields lose marks.

Example 3 · WHERE with a Text value (quotes required)

SELECT City, Population
FROM MajorCity
WHERE Continent = "Asia";

Returns: Abu Dhabi 1566999, Beijing 21766214, Jakarta 11248839.

Teaching point: "Asia" is Text — quotes required. Miss the quotes = zero marks on this line (cited: 2025 w25 examiner report).

Example 4 · WHERE with an Integer (no quotes)

SELECT City
FROM MajorCity
WHERE Population > 10000000;

Returns: Beijing, Buenos Aires, Cairo, Jakarta.

Teaching point: 10000000 is Integer — no quotes. Writing Population > "10000000" is marked wrong even if some databases accept it.

Example 5 · WHERE with a Boolean (no quotes — the trap)

SELECT City, Country
FROM MajorCity
WHERE Capital = TRUE;

Returns: every city where Capital is TRUE.

Teaching point: Boolean values are never quoted. WHERE Capital = "TRUE" is a mark-loser. The most-cited SQL exam mistake.

Example 6 · Two conditions with AND

SELECT City, Population
FROM MajorCity
WHERE Continent = "Asia" AND Population > 5000000;

Returns: Beijing (21.7M), Jakarta (11.2M). Abu Dhabi is filtered out — it's Asian but only 1.5M.

Teaching point: AND = both must be true. Every record is checked against both conditions.

Example 7 · ORDER BY (ascending, default)

SELECT City, Population
FROM MajorCity
ORDER BY Population;

Returns: every city, listed smallest population to largest. ASC is implied.

Example 8 · The full 2025 s25 shape

SELECT City, Country, Population
FROM MajorCity
WHERE Continent = "South America"
ORDER BY Population DESC;

Returns: Buenos Aires 15490415 (the only South American city in our sample). If more existed, they'd be sorted largest to smallest.

The full-query rule: SELECTFROMWHEREORDER BY;. Every clause on its own line. ORDER BY always after WHERE. Semi-colon at the end.

⚠️ Common misconceptions

Trap 1 · Missing quotes around Text values in WHERE

❌ Writing WHERE Country = China instead of WHERE Country = "China"

Text values in WHERE conditions must be in quotation marks (single or double, Cambridge accepts either). Without quotes the database treats the value as a field name — and if no field called "China" exists, the query fails. This is the single most-cited SQL mistake across Cambridge examiner reports 2023–2025.

Cited: 2023 Q9 BookList — "not to include quotation marks round the required author's name." · 2024 s24 Q9 SoftDrinks — "did not put quotation marks around 'Can' in the WHERE statement." · 2025 w25 — "missing off the quotation marks around the search criteria 'Asia'."

Trap 2 · Putting quotes AROUND Booleans or numbers

❌ Writing WHERE Capital = "TRUE" or WHERE Population > "1000000"

Booleans and numbers are never quoted in SQL. Only text-like values (Text, Character, Date/Time) get quotes. Writing "TRUE" turns a Boolean into a text string — and some databases won't match it against the actual Boolean value in the table.

Cited: 2025 w25 examiner report — mark scheme model SQL uses WHERE Capital = TRUE (no quotes).

Trap 3 · Extra punctuation in the query

❌ Adding a comma after the last field, adding punctuation inside SELECT, or ending a line mid-clause

SELECT City, Country, (trailing comma) is a syntax error. SELECT City. Country (period instead of comma) is a syntax error. Cambridge examiner reports flag "extra punctuation" as a recurring mark loss.

Cited: 2023 examiner report — "A common error seen was to include extra punctuation."

Trap 4 · Extra punctuation in the OUTPUT

❌ Adding commas or full stops when writing out what a query returns

When asked to "write the output of this SQL statement", students often add commas between the returned values. Cambridge outputs are plain values, one record per line, separated only by spaces or tabs — never punctuation.

Cited: 2024 w24 — "Punctuation marks should not be in the output from an SQL statement." · Reinforced 2025 m25 — same wording.

Trap 5 · Selecting the wrong field

❌ Writing a field name that sounds right but isn't actually the field the question asks for

The 2024 w24 examiner report flagged Airworthy as a common incorrect field in the SELECT line — students picked a nearby field instead of reading the question. Always re-check: which fields does the question actually ask for?

Cited: 2024 w24 Q10 Aircraft — "an incorrect field in the SELECT line. Airworthy was quite a common incorrect answer."

Trap 6 · Sorting on the wrong field, or in the wrong order

❌ Writing ORDER BY City when the question asked to sort by population, or writing ASC when DESC was needed

Read the question carefully — "ascending" vs "descending", "alphabetical" vs "numerical", and which field to sort on.

Cited: 2025 s25 Q8 MajorCity — "rows being sorted in the wrong order, for example, with Buenos Aires being in the first row rather than Valencia."

Trap 7 · Case-sensitive field names

❌ Writing SELECT genre FROM Books when the field is Genre

SQL field names and table names are case-sensitive. If the table shows BookName, your query must say BookName — not bookname, not Book_Name, not Book Name. Copy the field name exactly as it appears.

Cited: textbook 9.3 direct — "The case, spellings and order are all important."

Trap 8 · ORDER BY placed before WHERE

❌ Writing SELECT ... FROM ... ORDER BY ... WHERE ...

The clause order is fixed: SELECTFROMWHEREORDER BY. If both are used, WHERE must come first. Reversing them is a syntax error.

Cited: textbook 9.5 direct — "The order by statement comes after the SELECT ... FROM or SELECT ... FROM ... WHERE."

🎯 Cambridge exam focus

Mark-scheme-phrased chips. The exam-safe habits.

Quote strings, never quote Booleans or numbers. "China" yes. TRUE yes. "TRUE" no. "3.99" no.
Cambridge accepts both "double" and 'single' quotes on Text and Character values. Pick one and stay consistent within a query.
End every statement with ; — every Cambridge past-paper SQL example does. Missing it may lose the mark; including it never hurts.
Clause order is fixed: SELECTFROMWHEREORDER BY. Never rearrange.
Field names are case-sensitive. Match the table exactly. Genregenre.
No SELECT * in this syllabus. Always list the fields explicitly, even if you want all of them.
ASC is the default — writing it explicitly is fine but not required. DESC must always be written.
<> is "not equal to" — SQL does not use !=. That's a programming operator, not an SQL one.
Fields in WHERE don't have to appear in SELECT. You can filter on Continent without displaying Continent.
Cambridge syllabus uses AND and OR only — no NOT in this specification.
When writing output, no punctuation. No commas, no periods, no quotation marks. Just the values, one record per line.

🧪 Quick knowledge check

Click each question to reveal the answer.

1. What are the three required parts of every SQL query (before optional clauses)?
SELECT (which fields), FROM (which table), and a ; at the end. WHERE and ORDER BY are optional.
2. What symbol separates fields in a SELECT clause, and is it needed after the last field?
Commas separate fields. No trailing comma after the last field — that's a syntax error.
3. In WHERE Country = "China", why is China in quotation marks?
Because China is a Text value, and Text values must be quoted in SQL.
4. In WHERE Capital = TRUE, why is TRUE NOT in quotation marks?
Because TRUE is a Boolean value, and Booleans are never quoted in SQL.
5. What are the six logical operators used in WHERE clauses?
= (equal to), <> (not equal to), < (less than), <= (less than or equal to), > (greater than), >= (greater than or equal to).
6. What is the default sort order for ORDER BY if you don't specify?
Ascending (ASC). You don't need to write it — but writing it doesn't cost marks.
7. In a full query, which order do the clauses go in?
SELECTFROMWHEREORDER BY;. WHERE always comes before ORDER BY.
8. Write a complete SQL query that lists the city name and country of every capital city in Asia, sorted by population from largest to smallest.
SELECT City, Country
FROM MajorCity
WHERE Continent = "Asia" AND Capital = TRUE
ORDER BY Population DESC;
Key checks: "Asia" quoted (Text), TRUE unquoted (Boolean), DESC explicit, ORDER BY after WHERE, ; at the end.

✅ Ready for the next lab

You can now write a complete SQL query. You know what SELECT, FROM, WHERE, and ORDER BY do, when to use quotes, and what order the clauses go in. This is the core skill Cambridge tests every year.

Next lab: 9.6–9.7 Aggregating Lab — where you learn the two functions that let SQL do maths for you: SUM (adds up a field) and COUNT (counts records). Same query skeleton you've just learned — plus one extra bit of syntax. See you in Lab C.

🎓 Learn — walking-around understanding

Book Notes is the reference. Learn is the walk-through. If SQL is new, start here and go slow. If Book Notes already clicked, skim.

Concept 1 · A query is a sentence you speak to the table

Every SQL query you'll ever write in this syllabus is one sentence built from up to four fixed parts, in this fixed order:

SELECT   which columns
FROM     which table
WHERE    which records          (optional)
ORDER BY how to sort them       (optional)
  • Required: SELECT and FROM. Every query has these two.
  • Optional: WHERE filters records. ORDER BY sorts them.
  • Fixed order: if you use both optional clauses, WHERE comes before ORDER BY. Always.

Read a query left-to-right, top-to-bottom

The database does the same. It picks the table (FROM), filters the records (WHERE), sorts them (ORDER BY), then picks the columns to show (SELECT). You write it in exam order; the database executes it in that logical order.

Concept 2 · SELECT picks columns — the order you write is the order you get

Fields listed in SELECT are returned in the exact order you write them, separated by commas.

📝

SELECT City, Country gives you the city first, then the country.

🔁

SELECT Country, City flips it — country first, then city. Same data, wrong column order = lost marks.

Comma rules:

  • Commas between fields. Never after the last field.
  • No comma between FROM and the table name.
  • Cambridge doesn't teach SELECT *. Always list fields explicitly.

Vocabulary lock: in databases, columns are called fields. When the question says "output the fields", it means "show these columns".

Concept 3 · WHERE filters — and quotes are worth marks

WHERE narrows the table down. Every WHERE clause has the same shape: field · operator · value. Example: WHERE Cost < 3.99.

The trap is on the value. Whether it takes quotes depends entirely on its data type:

💬

Quote it: Text, Character, Date/Time.
WHERE Country = "China". WHERE Grade = 'A'.

🔢

Don't quote: Integer, Real, Boolean.
WHERE Price > 5. WHERE Capital = TRUE.

The one-line rule

"If the value is text-like, quote it. If it's number-like or True/False, don't." This rule alone is worth more exam marks than any other in Topic 9.

Two conditions? Join them with AND (both must match) or OR (either matches). Cambridge doesn't test NOT in this syllabus.

Concept 4 · ORDER BY sorts — pick a field and a direction

ORDER BY takes two pieces of information: which field to sort on, and which direction.

  • Ascending (ASC) is the default. Small → large. A → Z. You don't have to write ASC, but you can.
  • Descending (DESC) is the opposite. Large → small. Z → A. You must write it explicitly.

Placement: always last, always after WHERE if WHERE is present. Put ORDER BY in the middle and the query breaks.

Read the direction word carefully

Cambridge questions bury the sort direction in a phrase. "Highest to lowest" → DESC. "In alphabetical order" → ASC. "Youngest first" (biggest DOB numeric) → DESC. Every year, students lose the mark by defaulting to ASC when the question wanted DESC.

You've got Learn — head to Activities

Time to write real queries at Query Bench — a live SQL playground where the database runs your query, shows the rows it returns, and lints the exam-critical rules.

🎮 Query Bench

Pick a scenario, read the brief, then write the SQL to answer it. The bench runs your query against a live sample table, shows you the rows it returns, and lint-checks it against the Cambridge mark scheme rules that examiners cite every sitting.

📚 BookList · 2023
🥤 SoftDrinks · 2024 s24
🌍 MajorCity · 2025 s25
🌳 NATIONAL_TREES · 2026
📋 The table (sample data)
🎯 Task
✏️ Your SQL
👀 Result

Run your query to see the rows it returns.

🎯 Cambridge marker

Feedback lands here after you run — mark-scheme-style checks on quoting, clause order, spelling, and matched rows.

What Query Bench is testing

  • Do the SELECT fields match the task? Missed a field, added an extra, or spelled one wrong = mark loss (2024 w24 Airworthy trap).
  • Are your WHERE values quoted correctly? Text quoted, Booleans and numbers unquoted. Cited every sitting from 2023 through 2025.
  • Is the clause order right? SELECTFROMWHEREORDER BY. Reversing any pair is a syntax error.
  • Do the returned rows match the mark scheme? The bench checks both the shape of your query and the data it returns.

✎ Practice

Multiple-choice checks. Start on Easy if you're new to the topic; move up as you go. Every attempt is logged in Progress.

All
Easy
Medium
Exam-style

📋 Exam-style questions

Real past-paper style. Type your answer, then self-mark against the mark scheme. Skills feed into Mastery.

Exam mode active — choices hidden. Write in the box, then reveal the mark scheme.

🔄 Review

Your mistakes, ranked by frequency. The Vault fills up as you Save from Practice and Exam.

Mistakes to revisit

No mistakes logged yet. Come back after Practice and Exam.

Recent Vault entries

Nothing in the Vault yet. Save key facts from Book Notes or from question feedback.

🏆 Mastery

Every skill you attempt (in Query Bench, Practice, or Exam) is scored. Get 3+ attempts with 70%+ accuracy on a skill and it goes green — mastered.

No attempts yet.

Achievements

Complete Practice and Exam to unlock achievements.

Mistake log

Mistakes

Your recent mistakes

Progress

Stats & progress

Practice

0Attempts
Accuracy

Exam

0Attempts
Self-marked avg
Bookmarks

Bookmarked questions

No bookmarks yet. Star a question in Practice or Exam to save it here.

My notes

My notes

Add note

Title
Note

Your notes

Knowledge Vault 2.0

Knowledge Vault

Add entry

Category
Confidence
Title
Info

Entries

Saved ✓