SQL queries, conditions and joins
Read and write the specified SQL statements, predict query results and correct common errors in single- and two-table queries.
Mapped to the Cambridge IGCSE Computer Science 0265 syllabus for exams in 2029. These original explanations and self-checks are independent study support, not Cambridge papers, official marks or grades. Practical programming is taught with worked code and trace tasks; an executable Python workspace and timed digital-exam simulation are not yet available.
Before you begin
- Use the 2029 syllabus for this course. Cambridge specifies Python 3.10 or higher for programming and digital exams for both components. The syllabus currently identifies this route for 2029 only; confirm later-year availability with Cambridge and your school.
Work on paper. Try each question before revealing help, and explain why your method works.
Quick readiness check
Before sql queries, conditions and joins, which statement is the safest starting point?
What you will learn
- Retrieve rows with SELECT, FROM and WHERE
- Use sorting, conditions, aggregates and INSERT INTO
- Join related tables and explain query results
A query states what to retrieve and from where
SELECT chooses fields, FROM names a table and WHERE filters rows using one or more conditions. AND requires both conditions; OR requires either. Use quoted text values where appropriate and match each field to its table.
ORDER BY sorts the returned rows ascending or descending. COUNT counts records and SUM adds values in a field. INSERT INTO adds a record using named fields and values; the data types and validation rules still apply.
Joins combine related tables
INNER JOIN returns rows where the join condition matches in both tables. ON states which key fields are compared. Use table names or aliases to avoid ambiguity when both tables have a field with the same name.
To explain a query result, identify the source tables, selected fields, filter conditions, join matches and order. To correct a query, check spelling, field names, quotes, condition operators and whether the join key is appropriate.
Pause and explain
Which habit best checks your understanding of sql queries, conditions and joins?
Worked example
Write SQL to show names of learners with a score at least 80, ordered by score from highest to lowest.
Show the worked solution
- Select the required name and score fields from Results.
- Use WHERE Score >= 80 to filter.
- Use ORDER BY Score DESC to place the highest first.
Answer SELECT Name, Score FROM Results WHERE Score >= 80 ORDER BY Score DESC;
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.
Write SQL to select Title from Books where Price is below 20, ordered by Title ascending.
Give me a hint
Select one field, filter prices and sort the result.
Compare my reasoning
- Use SELECT Title FROM Books.
- Add WHERE Price < 20.
- Add ORDER BY Title ASC.
SELECT Title FROM Books WHERE Price < 20 ORDER BY Title ASC;
Look for these in your work
- Selected field
- Condition
- Ascending order
A query uses SELECT Name FROM Learner INNER JOIN Result; with no ON clause. Explain what is missing and correct the join if both tables share LearnerID.
Give me a hint
Specify which key values should match between the tables.
Compare my reasoning
- The query has no join condition.
- Add ON Learner.LearnerID = Result.LearnerID.
- Select fields with table qualification if names could be ambiguous.
SELECT Learner.Name FROM Learner INNER JOIN Result ON Learner.LearnerID = Result.LearnerID;
Look for these in your work
- Missing condition identified
- Keys matched
- Query corrected
A table has scores 60, 75 and 90. Explain the results of SELECT COUNT(Score), SUM(Score) FROM Results WHERE Score >= 70;.
Give me a hint
Filter first, then count and sum the matching values.
Compare my reasoning
- The WHERE clause keeps 75 and 90; 60 is excluded.
- COUNT(Score) counts two non-null matching score values.
- SUM(Score) adds 75 + 90 = 165.
COUNT = 2; SUM = 165.
Look for these in your work
- Rows filtered
- Count correct
- Sum correct
Common mistakes
- Putting a filter condition in ORDER BY.
- Using INNER JOIN without an ON condition.
- Forgetting that WHERE filters before the result is displayed.
What can you explain now?
Which SQL clause filters rows according to a condition?
Compare with the explanation
WHERE
WHERE selects rows that satisfy the specified condition.
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 SELECT, WHERE, ORDER BY, aggregates and INNER JOIN work together in a query?
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.