AND, OR and NOT
Stage 2: Filtering · Lesson 6 of 17 · 5 min read · free, interactive
Combine conditions with AND (both must be true) and OR (at least one is true). NOT flips a condition.
SELECT name, country, level FROM players WHERE country = 'VN' AND level >= 10;
SELECT name, country FROM players WHERE country = 'JP' OR country = 'US';
AND binds tighter than OR, just like × before + in maths. This query does not mean what it looks like:
SELECT name, country, level FROM players WHERE country = 'VN' OR country = 'JP' AND level >= 8;
It reads as: VN, or (JP and level ≥ 8).
Use parentheses to say what you mean:
SELECT name, country, level FROM players WHERE (country = 'VN' OR country = 'JP') AND level >= 8;
Practice
Each answer runs in your browser and is checked on hidden data.
- Write the query: Return the
nameof players who are from theUSor at level 12 or higher. Row order does not matter. - Find the bug: Return the
nameof players fromVNorJPwho are at level 8 or higher. Row order does not matter. - Write the query: Return the
nameandpriceof items that are not potions and cost less than 10. Row order does not matter.
Previous: Missing values: NULL · Next: Lists and ranges: IN and BETWEEN · All lessons