datarekha

Filtering with WHERE

How to keep only the rows you care about — comparisons, ranges, lists, patterns, and the logic that combines them.

6 min read Beginner SQL Lesson 2 of 27

What you'll learn

  • The comparison operators you'll use every day
  • BETWEEN, IN, and LIKE — what each is really doing
  • How AND, OR, NOT combine, and why parentheses matter
  • Where NULL quietly breaks a filter

Before you start

Almost no query you write at work wants the whole table. It wants this customer, that month, those products. WHERE is how you say which rows to keep: it runs once per row, evaluates a true/false test (a predicate), and keeps only the rows that come out true.

We will filter this small product table all lesson:

idnamecategoryprice
1PencilStationery2
2NotebookStationery6
3CoffeeGrocery9
4LaptopElectronics900
5CableElectronics12

Comparisons

The operators are what you would guess: =, <> (or !=), <, <=, >, >=. Strings compare alphabetically, dates chronologically, numbers numerically.

SELECT name, price
FROM   products
WHERE  price > 10;
nameprice
Laptop900
Cable12

BETWEEN, IN, and LIKE

BETWEEN a AND b is shorthand for >= a AND <= b, and both ends are included — a frequent surprise. IN (...) checks membership in a list, far cleaner than chaining ORs. And LIKE matches a text pattern, where % stands for any run of characters and _ for exactly one:

SELECT name, category, price
FROM   products
WHERE  name LIKE 'C%';        -- starts with C
namecategoryprice
CoffeeGrocery9
CableElectronics12

For dates, the half-open range >= '2024-03-01' AND < '2024-04-01' is the idiom analysts prefer over BETWEEN, because it composes cleanly across month and year boundaries without you ever having to remember how many days a month has.

Combining filters — and the precedence trap

You join conditions with AND, OR, and NOT. The one rule that trips people: AND binds tighter than OR, so A OR B AND C means A OR (B AND C). When in doubt, parenthesise.

SELECT name, category, price
FROM   products
WHERE  (category = 'Stationery' OR category = 'Grocery')
  AND  price < 10;
namecategoryprice
PencilStationery2
NotebookStationery6
CoffeeGrocery9

Drop those parentheses and the query quietly changes meaning to “all Stationery, plus Grocery under 10” — every pencil and notebook regardless of price. Same words, different answer, real bug.

Practice

Quick check

0/3
Q1Which WHERE clause correctly finds all orders placed in February 2024?
Q2Why might WHERE country NOT IN ('IN', 'US', NULL) return zero rows?
Q3WHERE category = 'Stationery' OR category = 'Grocery' AND price < 10 — what does it return?

Sign in to track your progress

Completed lessons, your XP, level, and streak save to your account — it's free and takes a few seconds.

Practice this in an interview

All questions
Given a query that filters on both a raw column and an aggregate result, how do you structure it for correctness and performance?

Raw-column filters belong in WHERE so the engine scans fewer rows before grouping. Aggregate filters must go in HAVING. Applying a filter in HAVING that could have been in WHERE forces the engine to aggregate more rows than necessary.

How does operator precedence work with AND and OR in a WHERE clause?

AND binds more tightly than OR, just like multiplication binds more tightly than addition. Without parentheses, the expression A OR B AND C is evaluated as A OR (B AND C), not (A OR B) AND C. Always use parentheses when mixing AND and OR to make intent explicit.

What is the difference between WHERE and HAVING in SQL, and when must you use HAVING?

WHERE filters individual rows before any grouping occurs; HAVING filters groups after GROUP BY is evaluated. You must use HAVING when the filter condition references an aggregate function like SUM or COUNT.

When would you use IN versus BETWEEN in a WHERE clause?

Use IN for a discrete set of values; use BETWEEN for a contiguous range. BETWEEN is inclusive on both ends. IN does not imply any order and is ideal for filtering against a known list, while BETWEEN is cleaner for numeric or date ranges.

How does the LIKE operator work in SQL, and when is it a performance problem?

LIKE matches string patterns using % (any sequence of characters) and _ (exactly one character). A leading wildcard like '%smith' forces a full table scan because the index cannot be used from the left side; a trailing wildcard like 'smith%' can use a B-tree index prefix scan.

What is the difference between WHERE and HAVING in SQL?

WHERE filters individual rows before any grouping or aggregation occurs; HAVING filters groups after aggregation. You cannot reference an aggregate function like COUNT() or SUM() in a WHERE clause because those values don't exist yet at that stage of query execution.

Related lessons

Explore further

Skip to content