Code of the Day
BeginnerQuerying basics

Filtering with WHERE

Keep only the rows you want — comparisons, AND/OR, IN, BETWEEN, LIKE, and NULL.

SQLBeginner9 min read
Recommended first
By the end of this lesson you will be able to:
  • Filter rows with WHERE and comparison operators
  • Combine conditions with AND / OR
  • Use IN, BETWEEN, LIKE, and IS NULL

WHERE keeps only the rows that match a condition. It comes after the table and before sorting:

SQL — editable, runs in your browser

Comparisons and combining

The usual operators work: =, <> (not equal), <, >, <=, >=. Combine conditions with AND and OR (use parentheses when mixing them):

SQL — editable, runs in your browser

String values go in single quotes ('US'). Double quotes mean something different in SQL (an identifier), so "US" will usually error or misbehave.

Handy shorthands

  • IN — match any of a list: country IN ('US', 'NL').
  • BETWEEN — an inclusive range: price BETWEEN 2 AND 5.
  • LIKE — pattern match, where % is any text: name LIKE 'A%' (starts with A).
SQL — editable, runs in your browser

NULL is special

means "no value," and it doesn't compare with =. Use IS NULL / IS NOT NULL:

SELECT name FROM customers WHERE country IS NULL;   -- not  = NULL

country = NULL is never true — a classic SQL gotcha.

Where to go next

Now order and trim the results: sorting and limiting.

Finished reading? Mark it complete to track your progress.

On this page