Boolean Logic in WHERE (AND, OR, NOT) in SQL
By Owen Middleton · Updated September 2026 · Examples run on PostgreSQL 17
Before this WHERE Clause and Comparison Operators, NULL Semantics and IS NULL
Builds toward CASE WHEN Expressions
What are AND, OR, NOT in SQL?
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.
What do AND, OR and NOT each do in a WHERE clause?
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.
How does OR work in a SQL WHERE clause?
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.
How do you use NOT in SQL?
NOT inverts a condition. Useful when it's easier to describe what you don't want:
SELECT id, status, total_amount FROM orders WHERE NOT (status = 'delivered' OR status = 'cancelled')
Everything that isn't delivered and isn't cancelled comes through.
Does AND run before OR in SQL?
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?
Practice AND, OR, NOT in SQL
Brightlane's marketing team is launching a domestic renewal campaign and needs the current active US customer base as the audience list.
Write a query to return the name and email of every active customer registered in the United States.
Assumptions:
- The
customerstable contains every customer Brightlane has on file. - US customers are identified by
country = 'US'. - Active customers are identified by
is_active = true; deactivated customers haveis_active = false.
Output:
- One row per active US customer, with columns
nameandemail.
Schema · ecommerce5 tables? = nullable
Run previews · Check grades
Write a query, then run it to see results here.
The full breakdown walks through the shape, each clause, why this approach beats the alternatives, and the trap to avoid.
See the full worked solution9 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.
Start learning to practice all 9 AND, OR, NOT problems, with instant grading and mastery tracking.
Common questions about AND, OR, NOT
Can a query have two WHERE clauses?
No. A query gets one WHERE clause and a second is a syntax error. Join the conditions with AND or OR inside the single clause instead, which is the job those operators exist to do.
Does the order of two conditions joined by AND change the result?
No. AND is symmetric, so both orderings keep exactly the same rows. Which one you write first is a readability choice rather than a behavioural one.
What does NOT return when the condition is NULL?
NULL again. NOT turns true into false and false into true, but it cannot turn an unknown into something known, so NOT applied to NULL is still NULL. A row filtered on that condition is dropped, because WHERE keeps only what is true.
Why do parentheses matter when you mix AND and OR?
Because AND binds tighter than OR, so a mix without brackets groups the AND first. Written plainly, false AND false OR true comes out true, while forcing the OR to run first makes it false. Parentheses cost nothing and remove the guesswork.