Tier 1 · Foundations

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

By Owen Middleton · Updated September 2026 · Examples run on PostgreSQL 17

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 = 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.

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.

Check your understanding

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

Practice AND, OR, NOT in SQL

Practice · easy ecommerce · Brightlane

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 customers table contains every customer Brightlane has on file.
  • US customers are identified by country = 'US'.
  • Active customers are identified by is_active = true; deactivated customers have is_active = false.

Output:

  • One row per active US customer, with columns name and email.
Schema · ecommerce5 tables? = nullable
categories
idinteger
nametext
parent_id?integer
products
idinteger
nametext
category_id?integer
pricenumeric
stock_qtyinteger
attributes?jsonb
order_items
idinteger
order_id?integer
product_id?integer
quantityinteger
unit_pricenumeric
customers
idinteger
nametext
emailtext
city?text
countrytext
created_attimestamptz
is_activeboolean
orders
idinteger
customer_id?integer
ordered_attimestamptz
statustext
total_amountnumeric

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution

The full breakdown walks through the shape, each clause, why this approach beats the alternatives, and the trap to avoid.

See the full worked solution

9 AND, OR, NOT practice problems

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.

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.