Trace the output of a SQL query
Evaluate a query row by row and write only the selected fields from matching records.
Section 9 · Databases
Exam scope and assessment notes
This sequence follows Cambridge IGCSE Computer Science 0984, 2026–2028 syllabus Version 4, Topic 9. It covers single-table databases only and the specified SELECT, FROM, WHERE, ORDER BY, SUM, COUNT, AND and OR content. The official specimen also demonstrates NOT in a filter. Multi-table joins, GROUP BY, database implementation and the separate 2029–2031 syllabus are not mapped here. Practice is original, self-checked and does not award an official Cambridge grade. Full timed papers and human subject-teacher review remain outstanding. Cambridge states that 0984 has the same 2026–2028 subject content as 0478, with 9–1 grading. In June and November, Cambridge lists 0984 in Administrative zone 3 and for centres in Oman and the UAE. Confirm availability for your centre and exam series with Cambridge International. The separately published 2029–2031 syllabus changes to Python 3 only and removes pseudocode; this 2026–2028 route does not cover those changes.
Before you begin
- Be able to read a small table and compare values. Each lesson introduces the database and SQL terms it uses.
Work on paper. Try each question before revealing help, and explain why your method works.
Quick readiness check
What is the first useful step when predicting a query result?
What you will learn
- Apply WHERE to each record
- Combine filtering and selected fields
- Present result rows in the requested form
Trace one record at a time
Read the query's WHERE condition and test every table record against it. Keep only rows where the condition is true.
From those rows, copy the fields named after SELECT. Do not include fields that were not selected.
Then apply aggregate or ordering behavior
If ORDER BY appears, arrange the matching result rows by that field and direction. If SUM or COUNT appears, calculate the aggregate over the matching rows.
Use the given table exactly. Avoid inventing rows or assuming an unstated sort order.
Pause and explain
The query selects Title and WHERE SoldOut = FALSE. Which values should be written for a matching row?
Worked example
WORKSHOP rows: (W1, Art, 8), (W2, Coding, 12), (W3, Art, 10). Find the output of SELECT SessionID FROM WORKSHOP WHERE Topic = "Art" AND Seats >= 9;
Show the worked solution
- W1 has Art but only 8 seats, so it fails the second condition.
- W2 has enough seats but is Coding, so it fails the first.
- W3 satisfies both and contributes its SessionID.
Answer The result contains W3.
Try a topic test
Choose Easy, Normal or Difficult. Save your answers and open worked review after finishing.
Test this topic →From guided practice to a new situation
Write your answer and reasoning first. Use a hint only if you are stuck. The model solution is for self-checking; your written work is not automatically marked.
Rows: (A1, 6), (A2, 11), (A3, 8). What does SELECT ID FROM EVENTS WHERE Seats >= 8; return?
Give me a hint
Include rows with 8 or more seats, then project ID.
Compare my reasoning
- A1 has 6 and is excluded.
- A2 and A3 meet the range.
- Return their IDs.
A2 and A3.
Look for these in your work
- Boundary included
- Rows selected
A table has values (R1, TRUE), (R2, FALSE), (R3, TRUE). Find the result of SELECT ID FROM RECORDS WHERE Active = TRUE;
Give me a hint
Test the Boolean value in each row.
Compare my reasoning
- R1 matches.
- R2 does not.
- R3 matches.
R1 and R3.
Look for these in your work
- Boolean filter
- Projection
Given (S1, Jazz, No), (S2, Jazz, Yes), (S3, Drama, No), find the rows returned by SELECT ID FROM SHOWS WHERE Type = Jazz AND SoldOut = No;
Give me a hint
Both conditions must hold in the same row.
Compare my reasoning
- S1 is Jazz and not sold out.
- S2 is sold out.
- S3 is Drama.
Only S1.
Look for these in your work
- Same-row conditions
- Result identified
Common mistakes
- Applying OR instead of AND
- Copying every source field instead of selected fields
- Filtering after aggregating when the query filters before aggregation
What can you explain now?
When does a record appear in a result with AND?
Compare with the explanation
When every condition is true
Each connected condition must pass.
After trying it yourself, choose your next review. This is your self-assessment.
Your review choice appears on Today. Sign in to sync it across devices.
Make it stick
How do you trace a query without skipping records?
If you needed the solution, close it and solve the problem again from a blank page. A correct answer is stronger when you can explain the reason for each step.