SQL AND, OR and NOT: combine conditions in WHERE

AND and OR join several conditions in a WHERE clause, and NOT turns one around. That is how you find business customers in the USA, products that are expensive or out of stock, or every customer outside one country.

In short: WHERE country = 'USA' AND segment = 'Business' needs both conditions to be true. WHERE price > 200 OR stock = 0 needs only one. NOT reverses a condition. AND is evaluated before OR, so mixed conditions belong in parentheses: (a OR b) AND c.

Before you start: a single condition with WHERE and the comparison operators (SQL WHERE).

Try it now

Two conditions that must both hold. The customers table is shown above the editor.

Task Sales is running a campaign for business customers in the USA only. Show name and city of every customer whose country is USA and whose segment is Business.

customers 12 rows, 7 columns
customer_idnamecitycountrycustomer_sincesegmentphone
1Harbor Fremont PartnersBostonUSA2021-03-15Business617-555-0142
2Emily CarterChicagoUSA2022-07-01ConsumerNULL
3Riverside County OfficeRiversideUSA2020-11-20Government951-555-0110
4Raj PatelLeedsUK2023-01-09Consumer0113 496 0123
5Lakeview Hotel LtdManchesterUK2022-04-18Business0161 496 0456
6Olivia BrooksAustinUSA2024-02-27ConsumerNULL
7Greene & Shaw LLPLondonUK2021-09-05Business020 7946 0321
8Maple Grove High SchoolAustinUSA2023-08-14School512-555-0187
9Chloe EvansBostonUSA2025-05-30Consumer617-555-0199
10Northline Printing CoTorontoCanada2020-06-12Business416-555-0133
11Daniel KimSeattleUSA2024-10-03ConsumerNULL
12Westbrook Dental ClinicChicagoUSA2022-12-01Business312-555-0164

How AND, OR and NOT work

For each row, every condition comes out true or false. AND and OR combine two of those answers into one:

Condition ACondition BA AND BA OR B
truetruetruetrue
truefalsefalsetrue
falsetruefalsetrue
falsefalsefalsefalse

Think of AND as strict: every extra condition can only shrink the result. OR is generous: every extra condition can bring in more rows.

SELECT first_name, last_name, department, salary
FROM employees
WHERE department = 'Sales' AND salary > 48000;
first_namelast_namedepartmentsalary
TomBradleySales62400
MarcusAllenSales49200

Julia Reyes is in Sales too, but at 46800 she misses the salary condition. Swap AND for OR and you would get everyone in Sales plus everyone else earning more than 48000.

Here is OR with two values of the same column. Note that the column name is repeated in each condition:

SELECT first_name, last_name, department
FROM employees
WHERE department = 'Warehouse' OR department = 'Accounting';
first_namelast_namedepartment
SarahLongWarehouse
FelixTurnerWarehouse
NinaCooperAccounting
PaulWardAccounting

NOT goes in front of a condition and flips it. WHERE NOT country = 'USA' returns the four customers outside the US: three in the UK (Leeds, Manchester, London) and Northline Printing in Toronto. For a simple comparison, country <> 'USA' does the same and often reads better. NOT really earns its place in front of parentheses, and as NOT IN and NOT BETWEEN in the next lesson.

Practice: OR

This time one of two conditions is enough. Before you run it, guess how many products should come back.

Task Purchasing is checking two things at once: expensive items and empty shelves. Show name, price and stock of every product that costs more than 200 or whose stock is 0.

products 12 rows, 5 columns
product_idnamecategorypricestock
1Copy paper, 500 sheetsPaper4.99420
2Blue ballpoint pens, 10-packWriting3.49310
3Lever arch file, grayFiling2.79185
4Comfort office chairFurniture18912
5Standing deskFurniture4495
6Black ink cartridgePrinter supplies24.964
7Yellow sticky notes, 12 padsWriting6.990
8Mono laser printer L30Electronics2198
927-inch monitorElectronics27915
10Notebook A5, ruledPaper5.49140
11Black toner XLPrinter supplies89.922
12Cross-cut shredderElectronics1290

Parentheses: AND comes before OR

Just as multiplication comes before addition, AND takes priority over OR. When both appear without parentheses, AND grabs the conditions on either side of it first. This causes one of the most common bugs in SQL. Say you want business customers in Chicago or Boston:

SELECT name, city, segment
FROM customers
WHERE city = 'Chicago' OR city = 'Boston' AND segment = 'Business';
namecitysegment
Harbor Fremont PartnersBostonBusiness
Emily CarterChicagoConsumer
Westbrook Dental ClinicChicagoBusiness

Emily Carter is a consumer, yet she is in the list. The database read the condition as city = 'Chicago' OR (city = 'Boston' AND segment = 'Business'), so the segment was never checked for Chicago. Parentheses fix it:

SELECT name, city, segment
FROM customers
WHERE (city = 'Chicago' OR city = 'Boston') AND segment = 'Business';
namecitysegment
Harbor Fremont PartnersBostonBusiness
Westbrook Dental ClinicChicagoBusiness

Rule of thumb: as soon as AND and OR share a condition, add parentheses. Even when the default order happens to be right, the next person reading the query sees your intent at once. The full order is NOT first, then AND, then OR.

Practice: the parentheses trap

Same pattern, different cities. The check notices if the parentheses are missing.

Task Marketing wants consumers from Boston or Austin for a prize draw. Show name, city and segment of every customer whose city is Boston or Austin and whose segment is Consumer. Businesses, schools and government offices are not included, not even from those cities.

customers 12 rows, 7 columns
customer_idnamecitycountrycustomer_sincesegmentphone
1Harbor Fremont PartnersBostonUSA2021-03-15Business617-555-0142
2Emily CarterChicagoUSA2022-07-01ConsumerNULL
3Riverside County OfficeRiversideUSA2020-11-20Government951-555-0110
4Raj PatelLeedsUK2023-01-09Consumer0113 496 0123
5Lakeview Hotel LtdManchesterUK2022-04-18Business0161 496 0456
6Olivia BrooksAustinUSA2024-02-27ConsumerNULL
7Greene & Shaw LLPLondonUK2021-09-05Business020 7946 0321
8Maple Grove High SchoolAustinUSA2023-08-14School512-555-0187
9Chloe EvansBostonUSA2025-05-30Consumer617-555-0199
10Northline Printing CoTorontoCanada2020-06-12Business416-555-0133
11Daniel KimSeattleUSA2024-10-03ConsumerNULL
12Westbrook Dental ClinicChicagoUSA2022-12-01Business312-555-0164

Common mistakes

Forgetting the parentheses

WHERE city = 'Boston' OR city = 'Austin' AND segment = 'Consumer' only checks the segment for Austin. You get too many rows, and the exercise reminds you that "AND is evaluated before OR". Wrapping the OR part in parentheses fixes it.

Writing the column only once

WHERE city = 'Boston' OR 'Austin' reads like English, but SQL sees two conditions: city = 'Boston' and 'Austin' on its own. A bare piece of text counts as false in SQLite, so only the Boston rows survive. The exercise spots this and replies "After OR or AND, each condition needs a column again". Each condition needs a column, an operator and a value, or use IN.

AND where you mean OR

"Customers in Boston and Austin" means both groups in everyday speech. In SQL, city = 'Boston' AND city = 'Austin' asks for a row that is in both cities at once. There is no such row, so the result is empty. For each row, ask yourself: must it meet both conditions (AND), or is one enough (OR)?

NOT and empty values

WHERE NOT department = 'Warehouse' skips rows where department is empty (NULL). A comparison with NULL is neither true nor false, and NOT does not change that. More in NULL in SQL.

AND and OR in MySQL, PostgreSQL, SQL Server and SQLite

AND, OR, NOT and the order NOT, then AND, then OR are the same in all four databases; each manual lists them that way. The one trap is the symbol ||:

DatabaseWhat || means
MySQLa logical OR by default; with the PIPES_AS_CONCAT mode it joins text instead. MySQL also has XOR (exactly one of the two).
PostgreSQLjoins text: 'Post' || 'greSQL'
SQL Serverjoins text in SQL Server 2025 and Azure SQL; older versions do not have ||
SQLite (these exercises)joins text

So always spell out AND, OR and NOT. Then your query means the same thing everywhere.

FAQ

How many conditions can I combine?

As many as you need. Once you have three or four, put each condition on its own line and start the line with AND or OR. The logic is then easy to scan.

Does the order of the conditions matter?

Not for the result: a AND b returns the same rows as b AND a. What matters is which parts belong together, and parentheses decide that.

Is there a shorter way to write lots of ORs?

Yes. city = 'Boston' OR city = 'Austin' OR city = 'Chicago' becomes city IN ('Boston', 'Austin', 'Chicago'). That is the next lesson.

Can I put NOT in front of parentheses?

Yes. NOT (city = 'Boston' OR city = 'Austin') returns every customer who is in neither city. It is the same as city <> 'Boston' AND city <> 'Austin': flipping an OR turns it into an AND.

Up next: IN and BETWEEN

Long chains of OR and ranges like "from 100 to 300" are shorter and safer with IN and BETWEEN, and that includes date ranges such as a quarter.

Practice IN and BETWEEN

Write to us

Choose language

Select your preferred language