N006 Tier 1 · Foundations

Boolean Logic in WHERE (AND, OR, NOT) in SQL

The WHERE clause accepts a single boolean expression, and AND, OR, and NOT are the operators for combining multiple conditions into one. Their behavior follows three-valued logic, which means NULL in any condition affects how the combined expression evaluates.

AND, OR, and NOT let you combine multiple conditions in a single WHERE clause.

A single comparison is often not enough. Business questions rarely come in perfectly simple form. Your manager wants active customers in the US. Two things have to be true at once. Another analyst needs orders that are either pending or in-progress. A data quality run is looking for rows that match one condition but not another. Each of those needs more than one WHERE condition, and AND, OR, and NOT are how you connect them.

AND says both conditions must be true. OR says at least one must be true. NOT inverts whatever comes after it. Those three rules cover almost every compound filter you'll write.

The setup is the same as a single-condition WHERE. You just add the operator between your conditions:

SELECT name, email
FROM customers
WHERE country = 'US' AND is_active = true

SQL checks every row. Both conditions have to pass. A US customer who is inactive doesn't make it through. An active customer from the UK doesn't either. Only rows where both are true reach the SELECT.

OR is less strict — a row passes when at least one condition is true:

SELECT name, plan
FROM users
WHERE plan = 'starter' OR plan = 'free'

Any user on either plan passes. A user on 'enterprise' doesn't match either condition and gets dropped.

NOT inverts a condition. Useful when it's easier to describe what you don't want:

Everything that isn't delivered and isn't cancelled comes through.

The one thing that trips people up: AND runs before OR when you mix them without parentheses.

WHERE is_active = true AND country = 'US' OR country = 'CA' reads as (is_active = true AND country = 'US') OR country = 'CA'. Every Canadian customer passes, active or not. Parentheses fix it and cost nothing:

WHERE is_active = true AND (country = 'US' OR country = 'CA')

Any time you mix AND and OR in the same WHERE clause, use parentheses to make the grouping explicit.

Check your understanding

You write: WHERE is_active = true AND country = 'US' OR country = 'CA'. Which rows does this return?

Practice

9 Boolean Logic in WHERE (AND, OR, NOT) practice problems

These problems are part of the Boolean Logic in WHERE (AND, OR, NOT) lesson in SQLMaxx, with instant grading and a worked solution on each.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.

Practice, feedback, mastery, review. That's the loop that turns reading into real skill.

Start free

No account, no credit card. Start solving in under a minute.