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_id | name | city | country | customer_since | segment | phone |
|---|---|---|---|---|---|---|
| 1 | Harbor Fremont Partners | Boston | USA | 2021-03-15 | Business | 617-555-0142 |
| 2 | Emily Carter | Chicago | USA | 2022-07-01 | Consumer | NULL |
| 3 | Riverside County Office | Riverside | USA | 2020-11-20 | Government | 951-555-0110 |
| 4 | Raj Patel | Leeds | UK | 2023-01-09 | Consumer | 0113 496 0123 |
| 5 | Lakeview Hotel Ltd | Manchester | UK | 2022-04-18 | Business | 0161 496 0456 |
| 6 | Olivia Brooks | Austin | USA | 2024-02-27 | Consumer | NULL |
| 7 | Greene & Shaw LLP | London | UK | 2021-09-05 | Business | 020 7946 0321 |
| 8 | Maple Grove High School | Austin | USA | 2023-08-14 | School | 512-555-0187 |
| 9 | Chloe Evans | Boston | USA | 2025-05-30 | Consumer | 617-555-0199 |
| 10 | Northline Printing Co | Toronto | Canada | 2020-06-12 | Business | 416-555-0133 |
| 11 | Daniel Kim | Seattle | USA | 2024-10-03 | Consumer | NULL |
| 12 | Westbrook Dental Clinic | Chicago | USA | 2022-12-01 | Business | 312-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 A | Condition B | A AND B | A OR B |
|---|---|---|---|
| true | true | true | true |
| true | false | false | true |
| false | true | false | true |
| false | false | false | false |
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_name | last_name | department | salary |
|---|---|---|---|
| Tom | Bradley | Sales | 62400 |
| Marcus | Allen | Sales | 49200 |
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_name | last_name | department |
|---|---|---|
| Sarah | Long | Warehouse |
| Felix | Turner | Warehouse |
| Nina | Cooper | Accounting |
| Paul | Ward | Accounting |
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_id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Copy paper, 500 sheets | Paper | 4.99 | 420 |
| 2 | Blue ballpoint pens, 10-pack | Writing | 3.49 | 310 |
| 3 | Lever arch file, gray | Filing | 2.79 | 185 |
| 4 | Comfort office chair | Furniture | 189 | 12 |
| 5 | Standing desk | Furniture | 449 | 5 |
| 6 | Black ink cartridge | Printer supplies | 24.9 | 64 |
| 7 | Yellow sticky notes, 12 pads | Writing | 6.99 | 0 |
| 8 | Mono laser printer L30 | Electronics | 219 | 8 |
| 9 | 27-inch monitor | Electronics | 279 | 15 |
| 10 | Notebook A5, ruled | Paper | 5.49 | 140 |
| 11 | Black toner XL | Printer supplies | 89.9 | 22 |
| 12 | Cross-cut shredder | Electronics | 129 | 0 |
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';
| name | city | segment |
|---|---|---|
| Harbor Fremont Partners | Boston | Business |
| Emily Carter | Chicago | Consumer |
| Westbrook Dental Clinic | Chicago | Business |
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';
| name | city | segment |
|---|---|---|
| Harbor Fremont Partners | Boston | Business |
| Westbrook Dental Clinic | Chicago | Business |
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_id | name | city | country | customer_since | segment | phone |
|---|---|---|---|---|---|---|
| 1 | Harbor Fremont Partners | Boston | USA | 2021-03-15 | Business | 617-555-0142 |
| 2 | Emily Carter | Chicago | USA | 2022-07-01 | Consumer | NULL |
| 3 | Riverside County Office | Riverside | USA | 2020-11-20 | Government | 951-555-0110 |
| 4 | Raj Patel | Leeds | UK | 2023-01-09 | Consumer | 0113 496 0123 |
| 5 | Lakeview Hotel Ltd | Manchester | UK | 2022-04-18 | Business | 0161 496 0456 |
| 6 | Olivia Brooks | Austin | USA | 2024-02-27 | Consumer | NULL |
| 7 | Greene & Shaw LLP | London | UK | 2021-09-05 | Business | 020 7946 0321 |
| 8 | Maple Grove High School | Austin | USA | 2023-08-14 | School | 512-555-0187 |
| 9 | Chloe Evans | Boston | USA | 2025-05-30 | Consumer | 617-555-0199 |
| 10 | Northline Printing Co | Toronto | Canada | 2020-06-12 | Business | 416-555-0133 |
| 11 | Daniel Kim | Seattle | USA | 2024-10-03 | Consumer | NULL |
| 12 | Westbrook Dental Clinic | Chicago | USA | 2022-12-01 | Business | 312-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 ||:
| Database | What || means |
|---|---|
| MySQL | a logical OR by default; with the PIPES_AS_CONCAT mode it joins text instead. MySQL also has XOR (exactly one of the two). |
| PostgreSQL | joins text: 'Post' || 'greSQL' |
| SQL Server | joins 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.