S
Topic 9 Databases · Page 9.3–9.5

Querying Lab

Ask the database questions. SELECT, FROM, WHERE, ORDER BY — the four clauses at the heart of every database query question.

🧪 Exam Mode ON — recall from memory, then reveal

📚 Topic overview

In the Foundations Lab you learned how a database is shaped. In this lab you learn how to ask it questions. Every SQL query you write is one sentence built from up to four 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.

We teach these clauses together because exam questions usually combine them in one query. Learn them as one sentence, in a fixed order — and learn to read a query and say what it outputs, which the syllabus also asks for.

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

🎯 Learning objectives

Cambridge 0478 Topic 9 · outcome 4
Read, complete and write SQL scripts that query a single table using SELECT, FROM, WHERE, ORDER BY … ASCENDING / DESCENDING, AND and OR.
Identify the output an SQL statement gives from the contents of a table.
By the end of this lab you can
List the fields a question asks for with SELECT … FROM, in the right order.
Filter records with WHERE and the relational operators =, <>, <, <=, >, >=.
Join conditions with the logical operators AND and OR.
Put text values in quotes, and leave numbers and TRUE/FALSE without.
Sort results with ORDER BY … ASCENDING or DESCENDING.
Write the output of a query — one record per line, no punctuation.

📖 Key terminology

TermMeaning
QueryA request made to a database to return specific data.
SELECTLists the fields (columns) to show, separated by commas.
FROMNames the table to search.
WHEREGives a condition — only records that meet it are returned.
ORDER BYSorts the returned records on a field.
ASCENDINGSort direction: smallest to largest, A to Z. (Real databases also accept the short form ASC.)
DESCENDINGSort direction: largest to smallest, Z to A. (Short form: DESC.)
Relational operatorCompares two values and gives TRUE or FALSE: =, <>, <, <=, >, >=.
Logical operatorJoins conditions: AND (both must be true), OR (either can be true).
ConditionThe test in a WHERE clause — a field, an operator and a value (e.g. Cost < 8.00).
OutputThe values a query returns — written as plain values, one record per line.
; (semicolon)Marks the end of an SQL statement. Good practice to include it.

SQL keywords are usually written in UPPERCASE (SELECT, FROM, WHERE) so they stand out from the field names.

🧠 Core theory

1. The anatomy of a SELECT query

Every query has the same skeleton:

SELECT   <fields>
FROM     <table>
WHERE    <condition>
ORDER BY <field> ASCENDING | DESCENDING;
  • SELECT and FROM are needed in every query.
  • WHERE and ORDER BY are optional — but if both are used, WHERE comes first.
  • End the statement with ; — good practice, and the way SQL is usually shown in past papers.

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 BookList;

Output:
Matilda   R Dahl
Coraline   N Gaiman

Query B

SELECT Author, BookName
FROM BookList;

Output:
R Dahl   Matilda
N Gaiman   Coraline

The order in the query is the order in the answer.

  • Commas go between fields — never after the last field, and never between FROM and the table name.
  • SELECT * (every field) isn't in the syllabus list. Always name the fields you want.

3. Copy field and table names exactly

Write every field and table name exactly as it appears in the table — same spelling, same capitals, no added spaces. If the field is BookName, write BookName, not Book Name or Bookname.

  • A misspelt name is a different field — the database can't find it.
  • Many databases don't mind capitals, but copying names exactly is the safe habit.

SQL keywords (SELECT, FROM, WHERE) are usually written in UPPERCASE so they stand out from the field names.

4. WHERE — the six relational operators

OperatorMeaningExample
=Equal toWHERE Fiction = TRUE
<>Not equal toWHERE Container <> "Can"
<Less thanWHERE Cost < 8.00
<=Less than or equal toWHERE Cost <= 10
>Greater thanWHERE Volume > 330
>=Greater than or equal toWHERE Volume >= 500

Link: these are the same relational operators you met in Topic 8 (Programming). "Not equal to" is <> — not !=.

A field used in WHERE doesn't have to appear in SELECT: you can filter on Continent without showing it.

5. Quotes — text in, numbers and TRUE/FALSE out

Examiner reports have flagged missing quotation marks around text values (e.g. 2023, 2024 s24 and 2025 w25).

Data type of the valueQuotes?Example
TextYesWHERE Country = "China"
IntegerNoWHERE Population > 1000000
RealNoWHERE Cost <= 3.99
BooleanNo — a common trapWHERE Capital = TRUE

We use double quotes in our examples. You may also see single quotes ('China') in some books.

📎 Useful background

Different databases write character and date values in different ways (for example 'A' or "A", and #11/12/2019# or "11/12/2019"). If a question needs one, copy the style used in the question.

🗝️ One-line rule

"Words go in quotes. Numbers and TRUE/FALSE don't."

6. AND and OR — combining conditions

Logical operatorEffect
ANDOnly records that meet both conditions are returned (fewer records).
ORRecords that meet either condition are returned (more records).

Each condition needs its own field: Container = "Can" OR Container = "Bottle", not Container = "Can" OR "Bottle". NOT isn't in the syllabus SQL list.

7. ORDER BY — sorting the results

  • ORDER BY Field ASCENDING sorts smallest → largest, A → Z.
  • ORDER BY Field DESCENDING sorts largest → smallest, Z → A.
  • The syllabus uses the full words ASCENDING and DESCENDING. Real databases also accept the short forms ASC and DESC.
  • Most databases sort ascending if you leave the direction out — but write it, so your answer is clear.
  • ORDER BY comes last, after WHERE.

Exam trap: the 2025 s25 examiner report noted rows sorted in the wrong order. Check which field to sort on and which direction.

✏️ Worked examples

All the examples use one MajorCity sample table, in the style of a recent Paper 2 question. Same data every time — only the query changes.

The MajorCity sample table

Code   City          Capital  Country      Continent      Population
ASY6   Abu Dhabi     TRUE     UAE          Asia           1566999
EUY3   Amsterdam     TRUE     Netherlands  Europe         1174025
ASY1   Beijing       TRUE     China        Asia           21766214
SAY1   Buenos Aires  TRUE     Argentina    South America  15490415
AFY1   Cairo         TRUE     Egypt        Africa         22183200
EUN1   Frankfurt     FALSE    Germany      Europe         796437
ASY3   Jakarta       TRUE     Indonesia    Asia           11248839
SAY2   Valencia      FALSE    Venezuela    South America  1408965

Example 1 · One field, whole table

SELECT City
FROM MajorCity;

Returns: every city name, in the order they are stored. No WHERE = every record. No ORDER BY = no sorting.

Example 2 · Two fields — order matters

SELECT City, Population
FROM MajorCity;

SELECT Population, City
FROM MajorCity;

Same data, different column order. Follow the order the question asks for.

Example 3 · WHERE with a text value (quotes)

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

Output:
Abu Dhabi 1566999
Beijing 21766214
Jakarta 11248839

Teaching point: "Asia" is text, so it is in quotes. The 2025 w25 examiner report noted answers that missed these quotes.

Example 4 · WHERE with a number (no quotes)

SELECT City
FROM MajorCity
WHERE Population > 10000000;

Output:
Beijing
Buenos Aires
Cairo
Jakarta

Teaching point: 10000000 is an integer — no quotes, and no commas inside the number.

Example 5 · WHERE with a Boolean (no quotes)

SELECT City, Country
FROM MajorCity
WHERE Capital = TRUE;

Returns: every city where Capital is TRUE.

Teaching point: Boolean values are not quoted. WHERE Capital = "TRUE" compares with the text "TRUE", not the Boolean value — a common trap.

Example 6 · Two conditions with AND

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

Output:
Beijing 21766214
Jakarta 11248839

Teaching point: AND = both must be true. Abu Dhabi is in Asia but has fewer than 5 million people, so it is left out.

Example 7 · ORDER BY ASCENDING

SELECT City, Population
FROM MajorCity
ORDER BY Population ASCENDING;

Returns: every city, from the smallest population (Frankfurt) to the largest (Cairo).

Example 8 · All four clauses

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

Output:
Buenos Aires Argentina 15490415
Valencia Venezuela 1408965

Teaching point: WHERE filters first, then ORDER BY sorts what's left. Largest population first = DESCENDING.

Example 9 · Identify the output

SELECT City, Country
FROM MajorCity
WHERE Capital = FALSE
ORDER BY City ASCENDING;

Output:
Frankfurt Germany
Valencia Venezuela

How to write it: one record per line, values in the order of the SELECT fields, separated by spaces. No commas, full stops or quotation marks — the 2024 w24 examiner report noted that punctuation should not be in the output.

⚠️ Common misconceptions

Trap 1 · Missing quotes around text values in WHERE

❌ Writing WHERE Container = Can instead of WHERE Container = "Can"

Text values in a condition go in quotation marks. Without them, the database treats the value as a field name.

Cited: examiner reports 2023 (BookList), 2024 s24 (SoftDrinks — "Can") and 2025 w25 ("Asia").

Trap 2 · Quotes around Booleans or numbers

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

Numbers and TRUE/FALSE are not quoted. "TRUE" is a piece of text, not the Boolean value TRUE.

Common exam mistake

Trap 3 · Extra punctuation in the query

❌ A comma after the last field, or a full stop instead of a comma

SELECT City, Country, (trailing comma) and SELECT City. Country are both errors.

Cited: 2023 examiner report — extra punctuation.

Trap 4 · Punctuation in the OUTPUT

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

Write plain values — one record per line, values separated by spaces.

Cited: 2024 w24 examiner report — "Punctuation marks should not be in the output from an SQL statement."

Trap 5 · Selecting the wrong field

❌ Using a field that sounds right but isn't the one the question asks for

Re-read the question: which fields does it actually ask you to show, and in what order?

Cited: 2024 w24 Q10 examiner report — an incorrect field (Airworthy) in the SELECT line.

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

❌ ORDER BY City when the question asks to sort by population, or ASCENDING when it wants largest first

Check two things: which field, and which direction.

Cited: 2025 s25 Q8 examiner report — rows sorted in the wrong order.

Trap 7 · Names not copied exactly

❌ Writing Book Name or Bookname when the field is BookName

Copy field and table names exactly as they appear in the table — same spelling, same capitals, no spaces added.

Common exam mistake

Trap 8 · ORDER BY placed before WHERE

❌ Writing SELECT … FROM … ORDER BY … WHERE …

The order is fixed: SELECT → FROM → WHERE → ORDER BY.

Common exam mistake

🎯 Exam focus

Habits that keep marks safe.

Words in quotes; numbers and TRUE/FALSE without. "China" ✓ · TRUE ✓ · "TRUE" ✗ · "3.99" ✗
Clause order is fixed: SELECT → FROM → WHERE → ORDER BY.
Sort with the syllabus words: ORDER BY Population DESCENDING or ORDER BY City ASCENDING. Write the direction every time.
Copy names exactly. Field and table names as they appear in the table.
Name the fields. SELECT * isn't in the syllabus — list the fields the question asks for, in its order.
"Not equal to" is <>, not !=.
AND narrows, OR widens. Each condition needs its own field, operator and value.
Fields in WHERE don't have to appear in SELECT.
End with ; — good practice.
Writing an output? One record per line, values separated by spaces — no commas, full stops or quotation marks.

🧪 Quick knowledge check

Click each question to reveal the answer.

1. Which two clauses must every SQL query have?
SELECT (which fields) and FROM (which table). WHERE and ORDER BY are optional. End with ;.
2. What separates fields in a SELECT clause, and is it needed after the last field?
Commas separate fields. No comma after the last field.
3. In WHERE Country = "China", why is China in quotation marks?
Because it is a text value, and text values go in quotes.
4. In WHERE Capital = TRUE, why is TRUE NOT in quotation marks?
Because it is a Boolean value, and Booleans are not quoted.
5. Name the six relational operators used in WHERE conditions.
= (equal to), <> (not equal to), < (less than), <= (less than or equal to), > (greater than), >= (greater than or equal to).
6. Which words set the sort direction after ORDER BY?
ASCENDING (smallest first, A–Z) and DESCENDING (largest first, Z–A). Write the direction every time.
7. In a full query, which order do the clauses go in?
SELECT → FROM → WHERE → ORDER BY → ;
8. Write a query that lists the City and Country of every capital city in Asia, largest population first.
SELECT City, Country
FROM MajorCity
WHERE Continent = "Asia" AND Capital = TRUE
ORDER BY Population DESCENDING;
Checks: "Asia" quoted (text), TRUE not quoted (Boolean), DESCENDING written, ORDER BY after WHERE.
9. Using the MajorCity table above, what is the output of SELECT City FROM MajorCity WHERE Continent = "Europe";?
Amsterdam
Frankfurt
One record per line, no punctuation.

✅ 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. You can also read a query and write its output.

Next lab: 9.6–9.7 Aggregating Lab — where SQL does the maths for you: SUM (adds up a field) and COUNT (counts records). Same query skeleton you've just learned.

🎓 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.

Write it in this order

Think of it as one sentence: "SELECT these fields FROM this table WHERE this is true, ORDER BY this field." The clauses always go in this 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.
  • SELECT * isn't in the syllabus. Always list the fields.

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 values.
WHERE Country = "China".

🔢

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

The one-line rule

"Words go in quotes. Numbers and TRUE/FALSE don't."

Two conditions? Join them with AND (both must match) or OR (either matches). NOT isn't in the syllabus SQL list.

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

ORDER BY needs two things: which field to sort on, and which direction.

  • ASCENDING: small → large, A → Z.
  • DESCENDING: large → small, Z → A.
  • Write the direction every time. (Real databases also accept ASC and DESC.)

Placement: always last, after WHERE if there is one.

Read the direction words carefully

Questions often hide the direction in a phrase. "Highest to lowest" → DESCENDING. "In alphabetical order" → ASCENDING. "Most recent first" → DESCENDING. Students who rush often pick the wrong one.

You've got Learn — head to Activities

Time to write real queries at Query Bench — a live SQL playground that runs your query, shows the rows it returns and checks the exam-critical rules. Then try the Output Predictor.

🎮 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 checks it against the rules that matter in the exam.

📋 The table (sample data)
🎯 Task
✏️ Your SQL
👀 Result

Run your query to see the rows it returns.

🎯 Marker

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

🔮 Output Predictor

The exam also asks you to identify the output of an SQL statement. Read the query, use the table, and type exactly what it returns.

One record per line, values in the order of the SELECT fields, separated by spaces — no commas, full stops or quotation marks.

What Query Bench is testing

  • Do the SELECT fields match the task, in the right order? A missing, extra or wrong field costs marks (2024 w24 examiner report).
  • Are the WHERE conditions right? Field, operator and value — joined with AND or OR where needed.
  • Are your values quoted correctly? Text in quotes; numbers and TRUE/FALSE without.
  • Is the sort right? ORDER BY the right field, with ASCENDING or DESCENDING.
  • Does it return the right records? The bench runs your query and compares the rows with the model answer.

✎ Practice questions

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 Mode
🧪 Exam Mode active. Options are hidden — recall the answer, then reveal and self-mark.

📋 Exam-style questions

Paper 2 style: write a query, or write the output of one. Press Mark my answer — the marker runs your SQL and checks it point by point. Skills feed into Mastery.

🔄 Review

Traps that examiner reports have flagged on SQL queries, memory triggers, and your own mistakes.

⚠️ 10 traps to know

Read them before every mock exam.

1 · Missing quotes around text values

Text values in WHERE go in quotes: Container = "Can".

Cited: examiner reports 2023, 2024 s24 and 2025 w25.

2 · Quotes around Booleans or numbers

Capital = TRUE, Population > 1000000 — no quotes.

Common exam mistake

3 · Extra punctuation in the query

No comma after the last SELECT field; commas, not full stops, between fields.

Cited: 2023 examiner report.

4 · Punctuation in the output

Plain values, one record per line — no commas, full stops or quotation marks.

Cited: 2024 w24 examiner report.

5 · Wrong field in SELECT

Show exactly the fields the question asks for, in its order.

Cited: 2024 w24 Q10 examiner report.

6 · Wrong sort field or direction

Highest / tallest / Z–A first = DESCENDING. Lowest / A–Z first = ASCENDING.

Cited: 2025 s25 Q8 examiner report.

7 · Names not copied exactly

Copy field and table names exactly as shown in the table.

Common exam mistake

8 · ORDER BY before WHERE

SELECT → FROM → WHERE → ORDER BY.

Common exam mistake

9 · != instead of <>

"Not equal to" in SQL is <>.

Common exam mistake

10 · AND and OR mixed up

Container = "Can" AND Container = "Bottle" returns nothing — a drink can't be both. Use OR.

Common exam mistake

🧠 Memory triggers

🗝️ "S-F-W-O"

SELECT → FROM → WHERE → ORDER BY. Fixed order.

🗝️ "Words in quotes"

Words go in quotes. Numbers and TRUE/FALSE don't.

🗝️ "Up or down?"

ASCENDING climbs: 1 → 9, A → Z. DESCENDING drops: 9 → 1, Z → A.

🗝️ "AND narrows, OR widens"

AND needs both conditions true (fewer records). OR needs either (more records).

🗝️ "Copy, don't type"

Copy field and table names straight from the table.

🗝️ "Plain output"

One record per line, values in SELECT order, no punctuation.

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.

Tap a status to set it by hand.

No attempts yet.

Achievements

Complete Practice and Exam to unlock achievements.

📝 Revision checklist

Tick each item as you cover it.

➡️ Next up

The Aggregating Lab adds SUM and COUNT to the queries you can already write.

Mistake log

Mistakes

Your recent mistakes

Progress

Stats & progress

Practice

0Attempts
–Accuracy

Exam

0Attempts
–Average key points
Bookmarks

Bookmarked questions

No bookmarks yet. Bookmark a question in the Exam tab 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 ✓