Ask the database questions. SELECT, FROM, WHERE, ORDER BY — the four clauses at the heart of every database query question.
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.
SELECT, FROM, WHERE, ORDER BY … ASCENDING / DESCENDING, AND and OR.SELECT … FROM, in the right order.WHERE and the relational operators =, <>, <, <=, >, >=.AND and OR.ORDER BY … ASCENDING or DESCENDING.| Term | Meaning |
|---|---|
| Query | A request made to a database to return specific data. |
SELECT | Lists the fields (columns) to show, separated by commas. |
FROM | Names the table to search. |
WHERE | Gives a condition — only records that meet it are returned. |
ORDER BY | Sorts the returned records on a field. |
ASCENDING | Sort direction: smallest to largest, A to Z. (Real databases also accept the short form ASC.) |
DESCENDING | Sort direction: largest to smallest, Z to A. (Short form: DESC.) |
| Relational operator | Compares two values and gives TRUE or FALSE: =, <>, <, <=, >, >=. |
| Logical operator | Joins conditions: AND (both must be true), OR (either can be true). |
| Condition | The test in a WHERE clause — a field, an operator and a value (e.g. Cost < 8.00). |
| Output | The 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.
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.; — 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.
Fields listed in SELECT are returned in the order given, separated by commas. Same data, different query = different output columns.
SELECT BookName, Author FROM BookList;
Output:
Matilda R Dahl
Coraline N Gaiman
SELECT Author, BookName FROM BookList;
Output:
R Dahl Matilda
N Gaiman Coraline
The order in the query is the order in the answer.
FROM and the table name.SELECT * (every field) isn't in the syllabus list. Always name the fields you want.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.
SQL keywords (SELECT, FROM, WHERE) are usually written in UPPERCASE so they stand out from the field names.
| Operator | Meaning | Example |
|---|---|---|
= | Equal to | WHERE Fiction = TRUE |
<> | Not equal to | WHERE Container <> "Can" |
< | Less than | WHERE Cost < 8.00 |
<= | Less than or equal to | WHERE Cost <= 10 |
> | Greater than | WHERE Volume > 330 |
>= | Greater than or equal to | WHERE 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.
Examiner reports have flagged missing quotation marks around text values (e.g. 2023, 2024 s24 and 2025 w25).
| Data type of the value | Quotes? | Example |
|---|---|---|
| Text | Yes | WHERE Country = "China" |
| Integer | No | WHERE Population > 1000000 |
| Real | No | WHERE Cost <= 3.99 |
| Boolean | No — a common trap | WHERE Capital = TRUE |
We use double quotes in our examples. You may also see single quotes ('China') in some books.
'A' or "A", and #11/12/2019# or "11/12/2019"). If a question needs one, copy the style used in the question."Words go in quotes. Numbers and TRUE/FALSE don't."
| Logical operator | Effect |
|---|---|
AND | Only records that meet both conditions are returned (fewer records). |
OR | Records 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.
ORDER BY Field ASCENDING sorts smallest → largest, A → Z.ORDER BY Field DESCENDING sorts largest → smallest, Z → A.ASC and DESC.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.
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.
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
SELECT City FROM MajorCity;
Returns: every city name, in the order they are stored. No WHERE = every record. No ORDER BY = no sorting.
SELECT City, Population FROM MajorCity; SELECT Population, City FROM MajorCity;
Same data, different column order. Follow the order the question asks for.
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.
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.
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.
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.
SELECT City, Population FROM MajorCity ORDER BY Population ASCENDING;
Returns: every city, from the smallest population (Frankfurt) to the largest (Cairo).
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.
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.
❌ 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").❌ 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.
❌ 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.
❌ 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."❌ 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.❌ 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.❌ 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❌ Writing SELECT … FROM … ORDER BY … WHERE …
The order is fixed: SELECT → FROM → WHERE → ORDER BY.
Habits that keep marks safe.
"China" ✓ · TRUE ✓ · "TRUE" ✗ · "3.99" ✗SELECT → FROM → WHERE → ORDER BY.ORDER BY Population DESCENDING or ORDER BY City ASCENDING. Write the direction every time.SELECT * isn't in the syllabus — list the fields the question asks for, in its order.<>, not !=.WHERE don't have to appear in SELECT.; — good practice.Click each question to reveal the answer.
SELECT (which fields) and FROM (which table). WHERE and ORDER BY are optional. End with ;.SELECT clause, and is it needed after the last field?WHERE Country = "China", why is China in quotation marks?WHERE Capital = TRUE, why is TRUE NOT in quotation marks?WHERE conditions.= (equal to), <> (not equal to), < (less than), <= (less than or equal to), > (greater than), >= (greater than or equal to).ORDER BY?ASCENDING (smallest first, A–Z) and DESCENDING (largest first, Z–A). Write the direction every time.SELECT → FROM → WHERE → ORDER BY → ;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.SELECT City FROM MajorCity WHERE Continent = "Europe";?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.
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.
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)
SELECT and FROM. Every query has these two.WHERE filters records. ORDER BY sorts them.WHERE comes before ORDER BY. Always.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.
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:
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".
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.
"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.
ORDER BY needs two things: which field to sort on, and which direction.
ASCENDING: small → large, A → Z.DESCENDING: large → small, Z → A.ASC and DESC.)Placement: always last, after WHERE if there is one.
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.
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.
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.
Run your query to see the rows it returns.
Feedback lands here after you run — mark-scheme-style checks on quoting, clause order, spelling, and matched rows.
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.
ORDER BY the right field, with ASCENDING or DESCENDING.Multiple-choice checks. Start on Easy if you're new to the topic; move up as you go. Every attempt is logged in Progress.
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.
Traps that examiner reports have flagged on SQL queries, memory triggers, and your own mistakes.
Read them before every mock exam.
Text values in WHERE go in quotes: Container = "Can".
Capital = TRUE, Population > 1000000 — no quotes.
No comma after the last SELECT field; commas, not full stops, between fields.
Plain values, one record per line — no commas, full stops or quotation marks.
Cited: 2024 w24 examiner report.Show exactly the fields the question asks for, in its order.
Cited: 2024 w24 Q10 examiner report.Highest / tallest / Z–A first = DESCENDING. Lowest / A–Z first = ASCENDING.
Copy field and table names exactly as shown in the table.
Common exam mistakeSELECT → FROM → WHERE → ORDER BY.
!= instead of <>"Not equal to" in SQL is <>.
Container = "Can" AND Container = "Bottle" returns nothing — a drink can't be both. Use OR.
SELECT → FROM → WHERE → ORDER BY. Fixed order.
Words go in quotes. Numbers and TRUE/FALSE don't.
ASCENDING climbs: 1 → 9, A → Z. DESCENDING drops: 9 → 1, Z → A.
AND needs both conditions true (fewer records). OR needs either (more records).
Copy field and table names straight from the table.
One record per line, values in SELECT order, no punctuation.
No mistakes logged yet. Come back after Practice and Exam.
Nothing in the Vault yet. Save key facts from Book Notes or from question feedback.
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.
Complete Practice and Exam to unlock achievements.
Tick each item as you cover it.
The Aggregating Lab adds SUM and COUNT to the queries you can already write.
No bookmarks yet. Bookmark a question in the Exam tab to save it here.