HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
SQL Queries: SELECT, WHERE and ORDER BY
  1. 1SELECT and FROM: choosing the fields
  2. 2WHERE: choosing the records
  3. 3ORDER BY: sorting the output
  1. Computer Science
  2. /SQL Queries: SELECT, WHERE and ORDER BY
  3. /WHERE: choosing the records

Course 9.2·Lesson 2 of 3

WHERE: choosing the records

WHERE keeps only the records that meet a condition. You write conditions with the comparison operators, put text in quotes, use TRUE and FALSE for a Boolean field, and work out exactly which records a script outputs.

about 25 min1 simulation1 practice question
Lesson 2 of 3Show all lessonsHide
SQL Queries: SELECT, WHERE and ORDER BY
  1. 1SELECT and FROM: choosing the fields
  2. 2WHERE: choosing the records
  3. 3ORDER BY: sorting the output

Try the code and the simulations beside the lesson.

Only some of the records

The librarian now wants the titles of the nature books only. SELECT chooses the fields. To choose the records, we add WHERE and a condition.

sql
SELECT Title
FROM Books
WHERE Genre = 'Nature';

The output:

table
Title
Green Hills
Deep Blue
Snow Fox

The database looks at each record in turn. If the condition is true for that record, the record is output. If it is false, the record is left out. Here three books pass.

The Books table drawn as a grid. The three rows whose Genre is Nature (K2 Green Hills, K4 Deep Blue and K6 Snow Fox) are shaded green; the other rows are grey. An arrow labelled WHERE Genre = 'Nature' points to an output list with the three titles Green Hills, Deep Blue and Snow Fox.

Notice that Genre is in the condition but not in the output. The field in a WHERE condition does not have to be selected.

The comparison operators

  • = equal to
  • <> not equal to
  • > greater than, and < less than
  • >= greater than or equal to, and <= less than or equal to

These are the same operators as in pseudocode.

sql
SELECT Title, Pages
FROM Books
WHERE Pages > 250;

The output:

table
Title        Pages
Night Train  320
Lost Key     256
Deep Blue    410
Iron Bridge  288

Look at Lost Key with 256256256 pages. It is greater than 250250250, so it passes. A book with exactly 250250250 pages would not pass > 250, but it would pass >= 250.

Text, numbers and Booleans

  • Text goes inside quotes: WHERE Author = 'Silva'. This course uses single quotes, which every database accepts.
  • Numbers have no quotes: WHERE Pages < 200.
  • Booleans are compared with TRUE or FALSE: WHERE OnLoan = TRUE.
sql
SELECT BookID
FROM Books
WHERE OnLoan = TRUE;

The output:

table
BookID
K1
K4
K5

Not equal to works for text too. The script below outputs every book whose author is not Silva.

sql
SELECT Title
FROM Books
WHERE Author <> 'Silva';

The output:

table
Title
Night Train
Lost Key
Deep Blue
Iron Bridge

When nothing matches. A condition that no record meets gives no output at all. That is not an error.

Practise with new numbers

Try it in Python

The program runs three scripts with WHERE on the Books table and prints the output of each, with the number of records.

Read it first. Then run it. Do the outputs match the lesson?

1. Change Pages > 250 to Pages >= 288. Predict the titles, then run it.

2. Change 'Nature' to 'nature'. What happens? SQLite compares text exactly, so the small n matters.

Common mistakes

  • Text without quotes. WHERE Genre = Nature makes the database look for a field called Nature.
  • Quotes round a number. Write Pages > 250, not Pages > '250'.
  • > when the question says "at least". "At least 300300300" is >= 300.
  • Thinking the WHERE field must be selected. It does not have to be.

Remember these

  • WHERE keeps the records for which the condition is true.
  • The operators are =, <>, >, <, >= and <=.
  • Text goes in quotes, numbers do not, and a Boolean field is compared with TRUE or FALSE.

Check yourself

Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.

  1. Which titles does SELECT Title FROM Books WHERE Author = 'Adeyemi'; output?
  1. Which book IDs does SELECT BookID FROM Books WHERE Pages < 200; output?
  1. Write a script that outputs the Title of every book that is not on loan.
  1. How many records does WHERE Genre <> 'Mystery' let through?
  1. Find the error: SELECT Title FROM Books WHERE Genre = History;

References

  • Cambridge International, IGCSE Computer Science 0478 syllabus (exams in 2026, 2027 and 2028), section 9 Databases, 9.4 (WHERE), and section 4, the comparison operators of the pseudocode.

Enroll free in SQL Queries: SELECT, WHERE and ORDER BY

Every lesson is open to read. Enrolling also lets you:

  • Mark lessons done and carry on where you stopped.
  • Take the tests of this lesson and its course.
  • Count the course towards the certificate for its chapter.
No payment. You can leave at any time.

Problems (4)

A city farm keeps a table of its animals, Animals…Easy · High School (9–10) · by StemPy Lab
An airline desk weighs hand luggage and stores the masses in a table Bags, in kilograms…Medium · High School (9–10) · by StemPy Lab
A school books rooms from this table. Write a script that outputs the room number of every room…Medium · High School (9–10) · by StemPy Lab
Next: lesson 3ORDER BY: sorting the output
← SQL Queries: SELECT, WHERE and ORDER BY
← SELECT and FROM: choosing the fieldsORDER BY: sorting the output →

Discussion (0)

Log in to join the discussion.

Loading…

1 Python program
5 questions to check yourself

This lesson is free to read. Enroll free to keep your place and take the tests.

A teacher stores test results as (Student, Score): (Ama, 64), (Ben, 70), (Chloe, 88), (Dev, 69)…Hard · High School (9–10) · by StemPy Lab

Python Labs (1)

Choosing records with WHEREPython Lab · by StemPy Lab