Skip to content
Codewilds

Lesson 3 of 30 · Querying Basics

SQL WHERE: Filter Rows with Conditions

easy10 XP2 min readFree lesson

So far every query returned every row of a table. Most real questions are about some rows: the heroes above level 15, the quests in one region, the items under 100 gold. The WHERE clause keeps only the rows that match a condition.

Filtering with WHERE

WHERE comes after FROM. The database checks the condition for every row and keeps the rows where it is true:

SELECT name, level FROM heroes WHERE level > 18;
name level
Cyra Moonweaver 21
Hugo Stonebeard 23
Mira Sunstrider 19
Oona Fernwhistle 20

As a reminder, the heroes table has these columns:

id name class level gold guild_id joined
1 Aria Swiftwind Ranger 14 320 1 2025-01-12
2 Borin Ironfist Warrior 18 540 2 2024-11-03
6 Finn Oakheart Warrior 5 40 4 2025-06-01
… … … … … … …

Comparison operators

Operator Meaning Example
= equal to class = 'Mage'
<> not equal to (!= works too) class <> 'Rogue'
<, > less than, greater than gold < 100
<=, >= less or equal, greater or equal level >= 10

Text values go in single quotes: 'Mage'. Numbers don't need quotes. Text comparisons are case-sensitive — class = 'mage' finds nobody, because the table says Mage.

AND, OR and NOT

Combine conditions to ask more precise questions:

  • a AND b — both must be true.
  • a OR b — at least one must be true.
  • NOT a — flips true and false: NOT class = 'Rogue' means the same as class <> 'Rogue'.
SELECT name, class, level FROM heroes
WHERE class = 'Warrior' AND level >= 10;

This returns Borin Ironfist (18) and Hugo Stonebeard (23). Finn Oakheart is a Warrior too, but at level 5 he fails the second condition.

Parentheses

AND is evaluated before OR, just like * before + in arithmetic. Compare these two queries:

-- Mages and Healers, but only the ones with more than 300 gold
SELECT name, class, gold FROM heroes
WHERE (class = 'Mage' OR class = 'Healer') AND gold > 300;

This returns Cyra, Gwen and Oona. Without the parentheses, class = 'Mage' OR class = 'Healer' AND gold > 300 means "every Mage, plus Healers with more than 300 gold" — so Kira Frostvale, a Mage with only 20 gold, sneaks in. When a condition mixes AND and OR, always add parentheses, even when you think they aren't needed.

Common mistakes

  • Forgetting the quotes. WHERE class = Mage fails with no such column: Mage — without quotes, SQL thinks Mage is a column name.
  • Leaving out the column in the second comparison. WHERE level > 10 AND < 20 is a syntax error. Every comparison needs both sides: level > 10 AND level < 20.
  • > when you mean >=. "Level 14 or higher" includes 14, so it is level >= 14, not level > 14.
  • Mixing AND and OR without parentheses (see above).

Assignment

The Iron Wall guild is recruiting front-line fighters. Find every hero who is a Warrior or a Ranger and has a level of 14 or higher.

Return the columns name, class and level, in this order. The row order doesn't matter.

Keep learning

Need a quick reminder? The SQL cheat sheet has every topic of Learn SQL on one page.

Lesson last updated on .

Write code ↓
SQL+10 XP

Press Run to execute your code, or Submit to check it against the tests.