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.
Before this WHERE Clause and Comparison Operators, NULL Semantics and IS NULL
Builds toward CASE WHEN Expressions
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 = trueSQL 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.
You write: WHERE is_active = true AND country = 'US' OR country = 'CA'. Which rows does this return?
9 Boolean Logic in WHERE (AND, OR, NOT) practice problems
Write a query to return the name and email of every active customer registered in the United States.
Write a query to return the name and current plan of every user on either the `starter` or `free` plan.
Write a query to return the name and department ID of every employee assigned to either of those departments.
Write a query to return the ID and name of every UK customer who has no city on file.
Write a query to return the name and email of every active US customer who has a city on file.
Write a query to return the ID, status, and total amount for every order that is neither delivered nor cancelled.
Write a query to return the name and price of every product in the sweep.
Write a query to return the name and country of every qualifying customer.
Write a query to return the ID and name of every customer being reassigned.
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.
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.
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.
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.
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.
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 freeNo account, no credit card. Start solving in under a minute.