CodingNeed.

1 · Read the data · 12 MIN

Combine conditions without guessing precedence

AND requires both conditions; OR accepts either.

Complex filters are easier to understand when grouped with parentheses. The sample requires both a chosen city and enough points. AND binds more tightly than OR in SQL, but relying on remembered precedence can hide mistakes. Use parentheses to state the intended grouping and evaluate each row explicitly. A row with an unknown city does not satisfy city = Delhi. The next lesson explains why unknown values need a different comparison.

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

AND
Both conditions must be true.
OR
At least one condition must be true.
precedence
The order in which operators are grouped.

Follow the example step by step

  1. Evaluate city and points as two separate questions for each row.
  2. Combine with AND and retain only Grace.
  3. Use parentheses around the city alternatives for the change task.
  4. For the build task, either one of its two conditions is enough.
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 names where city is Delhi OR points is zero, ordered by id. The zero-point learner must be included even with an unknown city.

Read the example

SELECT name FROM learners
WHERE city = 'Delhi' AND points >= 30
ORDER BY id;
Check the expected output
[["Grace"]]

Your challenge

Return names where city is Delhi OR points is zero, ordered by id. The zero-point learner must be included even with an unknown city.

Common trap

A missing pair of parentheses can allow rows that should fail another condition.

Further reading: SQLite SELECT reference

Next lesson: Understand unknown values and NULL

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.