All posts
SQLNovember 20, 20267 min read

SQL AND, OR, NOT Operators Explained for Beginners

Learn SQL AND, OR and NOT operators with simple examples. See how to combine conditions in WHERE and avoid the common mistakes beginners make.

F
Fepiq Team
Fepiq

What are the SQL AND, OR and NOT operators? They are small words that let you combine several conditions in one WHERE clause. AND means every condition must be true. OR means at least one condition must be true. NOT flips a condition so that true becomes false. With them, you can ask a database real questions like "show me customers from Canada who spent more than 100 dollars".

This guide is for absolute beginners. We explain every term the first time we use it, and every example is small enough to copy and run. If you have not yet learned how a basic filter works, read our SELECT and WHERE guide first, then come back here.

A quick reminder: what WHERE does

SQL (Structured Query Language) is the language used to talk to databases. A database stores data in tables, which look like spreadsheets: rows go across, columns go down. The WHERE clause keeps only the rows that match a condition. A condition is a yes-or-no test, such as country = 'Canada'.

One condition is often not enough. That is where AND, OR and NOT come in. They are called logical operators because they work with true and false.

The sample table we will use

Imagine a table called customers with a few rows. You can create it in any SQL database, such as PostgreSQL, MySQL or SQLite.

sql
CREATE TABLE customers (
  id INT,
  name TEXT,
  country TEXT,
  total_spent INT
);

INSERT INTO customers (id, name, country, total_spent) VALUES
  (1, 'Ana', 'Canada', 150),
  (2, 'Ben', 'Canada', 40),
  (3, 'Chen', 'China', 220),
  (4, 'Dara', 'Kenya', 90),
  (5, 'Eli', 'Brazil', 300);

The first statement builds the table with four columns. The second adds five rows. Each name in parentheses after INSERT INTO is a column, and each line after VALUES is one row. Text goes inside single quotes; numbers do not.

The AND operator: both must be true

Use AND when you want rows that pass two or more tests at the same time.

sql
SELECT name, country, total_spent
FROM customers
WHERE country = 'Canada'
  AND total_spent > 100;

Line 1 picks the columns to show. Line 2 names the table. Line 3 is the first test: the country must be Canada. Line 4 adds a second test: total_spent must be more than 100. Only Ana passes both, so only Ana is returned. Ben is in Canada but spent only 40, so he is dropped.

The OR operator: one is enough

Use OR when a row only needs to pass one of the tests.

sql
SELECT name, country
FROM customers
WHERE country = 'Canada'
  OR country = 'Kenya';

This returns Ana, Ben and Dara. A row is kept if the country is Canada, or Kenya, or both. Here a row cannot be both, but in other queries it can. Notice that you must write the column name again after OR. Writing country = 'Canada' OR 'Kenya' is a common mistake and will not work.

The NOT operator: flip the result

NOT turns a true test into false, and a false test into true. It is useful for saying "everything except".

sql
SELECT name, country
FROM customers
WHERE NOT country = 'Canada';

This returns everyone who is not from Canada: Chen, Dara and Eli. You can get the same result with the not-equal operator, country <> 'Canada'. NOT is most useful together with other keywords such as NOT IN, NOT LIKE and NOT BETWEEN.

sql
SELECT name
FROM customers
WHERE country NOT IN ('Canada', 'China');

The IN list holds several values. NOT IN keeps rows whose country is not in that list, so you get Dara and Eli.

Truth table: AND vs OR vs NOT at a glance

OperatorRuleExampleResult
ANDAll conditions must be truecountry = 'Canada' AND total_spent > 100Ana
ORAt least one condition is truecountry = 'Canada' OR country = 'Kenya'Ana, Ben, Dara
NOTReverses the conditionNOT country = 'Canada'Chen, Dara, Eli

Combining AND and OR: the order matters

Things get tricky when you mix AND with OR. SQL checks AND before OR, just like multiplication comes before addition in maths. This is called operator precedence. Look at this query.

sql
SELECT name, country, total_spent
FROM customers
WHERE country = 'Canada'
   OR country = 'China'
  AND total_spent > 200;

You might think this means "Canada or China, with more than 200 spent". But SQL reads it as "Canada, or (China and more than 200)". So Ana and Ben (both in Canada, any spend) are returned, plus Chen. Ben appears even though he spent only 40, which is probably not what you wanted.

Use parentheses to be clear

Parentheses group conditions together. SQL works out what is inside them first. Always use them when you mix AND and OR.

sql
SELECT name, country, total_spent
FROM customers
WHERE (country = 'Canada' OR country = 'China')
  AND total_spent > 100;

Now the OR part is grouped. A row must be in Canada or China, and also have total_spent above 100. Ana and Chen pass. Ben does not. Even when parentheses are not required, they make your query easier for humans to read.

Common beginner mistakes

  • Forgetting to repeat the column name: write country = 'A' OR country = 'B', not country = 'A' OR 'B'.
  • Mixing AND and OR without parentheses, which gives surprising results.
  • Using AND when you mean OR. One country column cannot equal 'Canada' AND 'Kenya' at once, so that query returns nothing.
  • Putting numbers in quotes or text without quotes. Text needs single quotes.
  • Using NOT with NULL. Missing values (NULL) need IS NULL or IS NOT NULL instead of = or <>.

Practice challenges

  1. Find customers from Brazil or Kenya.
  2. Find customers who spent between 50 and 200 using AND with >= and <=.
  3. Find customers who are not from China and spent more than 100.
  4. Find customers from Canada or Kenya who spent less than 100, using parentheses.

Try each one yourself before looking for answers. Run the query, read the rows it returns, and ask whether they match what you expected. That habit is the fastest way to learn SQL.

Want to filter text more flexibly? Learn how the LIKE operator and wildcards work next.

Read the SQL LIKE guide

Frequently asked questions

What is the difference between AND and OR in SQL?+

AND keeps a row only when every condition is true. OR keeps a row when at least one condition is true. AND makes results smaller, while OR usually makes them larger.

Which runs first in SQL, AND or OR?+

AND runs before OR. To control the order, wrap the parts you want evaluated first in parentheses. This avoids surprising results.

Can I use AND and OR together in one WHERE clause?+

Yes. Just use parentheses to group them, for example WHERE (a = 1 OR b = 2) AND c = 3. This makes your meaning clear to both the database and other people.

How do I use NOT in SQL?+

Put NOT before a condition to reverse it, such as WHERE NOT country = 'Canada'. You can also use it with keywords like NOT IN, NOT LIKE, NOT BETWEEN and IS NOT NULL.

Why does my SQL query with OR return too many rows?+

OR keeps a row if any single condition matches, so it can let in more rows than expected. If you also use AND, check that parentheses group the conditions the way you intended.

Need help building a database-driven website or app? Talk to the Fepiq team.

Contact Fepiq

Get new posts in your inbox

Occasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.

Keep reading

Related posts

All posts

Let's build something

Ready to ship your next product with Fepiq?

Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.