CodingNeed.

1 · Read the data · 12 MIN

Keep matching rows with WHERE

WHERE keeps a row only when its condition is true.

Selection of columns and filtering of rows are different operations. Here WHERE checks points for every candidate row, while SELECT still chooses the output columns. The operator >= includes the boundary value. A quoted value such as Delhi is text; points is numeric. Filtering happens before ordering the remaining rows. Start with one condition and predict exactly which source rows pass before running it.

Keep the source rows visible. Predict which rows and columns will remain after each operation.

Before you start

No installation. The tables below are loaded automatically for every run; edits do not touch the CodingNeed account database.

New words, explained

predicate
A condition deciding whether to keep a row.
comparison
A test such as =, < or >=.
boundary
A value exactly on a cutoff, such as 20.

Follow the example step by step

  1. Check points in each source row.
  2. Keep Ada, Grace and Sam for >= 20.
  3. Change >= to > and predict why the two boundary rows disappear.
  4. Use <= for the exercise’s inclusive upper bound.
Practice table: learners
idnamecitypoints
1AdaDelhi20
2LinNULL (unknown)0
3GraceDelhi35
4SamPune20
Practice table: enrollments
learner_idcourse
1React
1SQL
3SQL

You are ready to move on when: Return name and points for learners whose points are at most 20, ordered by id. Include zero.

Read the example

SELECT name, points
FROM learners
WHERE points >= 20
ORDER BY id;
Check the expected output
[["Ada",20],["Grace",35],["Sam",20]]

Your challenge

Return name and points for learners whose points are at most 20, ordered by id. Include zero.

Common trap

WHERE points < 20 is not the same as at most 20.

Further reading: SQLite SELECT reference

Next lesson: Combine conditions without guessing precedence

Essential cookies keep your account signed in. Optional analytics is not configured on this site. Your choice does not affect access to lessons.

Read the Privacy Policy. You can change this choice in the footer.