SQL WHERE Clause: Multiple Conditions, IN, BETWEEN
· 7 min read
The SQL WHERE clause keeps only the rows where a condition is true and throws the rest away before anything else happens. It goes after FROM and before GROUP BY or ORDER BY.
SELECT order_id, customer_name, amount
FROM orders
WHERE region = 'North'
ORDER BY order_id;That returns North orders only. Everything you filter in WHERE is gone for the rest of the query, so totals, averages and counts only see the rows that passed. Below: combining conditions with AND and OR, WHERE IN, filtering between dates safely, and the bracket mistake that lets the wrong rows in.
A worked example
| order_id | customer_name | region | ordered_at | status | amount |
|---|---|---|---|---|---|
| 1 | Acme Dental | North | 2025-02-27 09:15:00 | completed | 320 |
| 2 | Birch Cafe | South | 2025-03-03 11:00:00 | completed | 150 |
| 3 | Cobalt Labs | North | 2025-03-14 16:45:00 | cancelled | 410 |
| 4 | Dune Studio | West | 2025-03-20 10:30:00 | completed | 275 |
| 5 | Elm Bakery | South | 2025-03-31 14:20:00 | refunded | 90 |
| 6 | Fern Florist | East | 2025-03-31 17:05:00 | completed | 205 |
| 7 | Acme Dental | North | 2025-04-02 08:40:00 | completed | 180 |
Run the query from the top on it:
SELECT order_id, customer_name, amount
FROM orders
WHERE region = 'North'
ORDER BY order_id;| order_id | customer_name | amount |
|---|---|---|
| 1 | Acme Dental | 320 |
| 3 | Cobalt Labs | 410 |
| 7 | Acme Dental | 180 |
Text values go in single quotes. Numbers do not: WHERE amount > 200. The comparison operators are =, <> (or !=), <, <=, > and >=.
Whether 'north' matches 'North' depends on the database. DuckDB and Postgres compare text exactly, so WHERE region = 'north' returns nothing here. MySQL and SQL Server usually ignore case by default. Copy values exactly as they appear in the data.
SQL WHERE with multiple conditions
AND: every condition must be true
SELECT order_id, customer_name, amount
FROM orders
WHERE region = 'North'
AND status = 'completed'
ORDER BY order_id;| order_id | customer_name | amount |
|---|---|---|
| 1 | Acme Dental | 320 |
| 7 | Acme Dental | 180 |
Cobalt Labs is in the North but its order was cancelled, so it fails the second condition.
OR: at least one condition must be true
SELECT order_id, customer_name, region
FROM orders
WHERE region = 'South' OR region = 'West'
ORDER BY order_id;| order_id | customer_name | region |
|---|---|---|
| 2 | Birch Cafe | South |
| 4 | Dune Studio | West |
| 5 | Elm Bakery | South |
Mixing AND and OR needs brackets
AND is evaluated before OR, just like multiplication before addition. When you mix them, wrap the OR part in brackets:
SELECT order_id, customer_name, region, status
FROM orders
WHERE (region = 'North' OR region = 'South')
AND status = 'completed'
ORDER BY order_id;| order_id | customer_name | region | status |
|---|---|---|---|
| 1 | Acme Dental | North | completed |
| 2 | Birch Cafe | South | completed |
| 7 | Acme Dental | North | completed |
What happens without the brackets is the first item in the mistakes section.
WHERE IN: match a list
IN is a shorter way to write several ORs on the same column. This returns exactly the same three rows as the bracketed query above:
SELECT order_id, customer_name, region, status
FROM orders
WHERE region IN ('North', 'South')
AND status = 'completed'
ORDER BY order_id;| order_id | customer_name | region | status |
|---|---|---|---|
| 1 | Acme Dental | North | completed |
| 2 | Birch Cafe | South | completed |
| 7 | Acme Dental | North | completed |
NOT IN excludes a list. WHERE status NOT IN ('cancelled', 'refunded') keeps orders 1, 2, 4, 6 and 7. Be careful if the column can be NULL: rows with a NULL status pass neither IN nor NOT IN. See SQL COALESCE and NULL for why. You can also put a query inside the brackets, WHERE customer_id IN (SELECT ...), which is covered in SQL subquery examples.
BETWEEN dates
BETWEEN a AND b means >= a AND <= b, with both ends included. On numbers it is simple: WHERE amount BETWEEN 150 AND 275 returns orders 2, 4, 6 and 7, including the 150 and the 275.
On dates it hides a trap. Here is "all March orders" written with BETWEEN:
SELECT order_id, ordered_at
FROM orders
WHERE ordered_at BETWEEN '2025-03-01' AND '2025-03-31'
ORDER BY order_id;| order_id | ordered_at |
|---|---|
| 2 | 2025-03-03 11:00:00 |
| 3 | 2025-03-14 16:45:00 |
| 4 | 2025-03-20 10:30:00 |
Orders 5 and 6 are missing, both placed on 31 March. The column holds a date and a time, and '2025-03-31' means midnight at the start of the 31st. Anything later that day is outside the range.
The safe pattern is "on or after the first day, and before the first day of the next period":
SELECT order_id, ordered_at
FROM orders
WHERE ordered_at >= '2025-03-01'
AND ordered_at < '2025-04-01'
ORDER BY order_id;| order_id | ordered_at |
|---|---|
| 2 | 2025-03-03 11:00:00 |
| 3 | 2025-03-14 16:45:00 |
| 4 | 2025-03-20 10:30:00 |
| 5 | 2025-03-31 14:20:00 |
| 6 | 2025-03-31 17:05:00 |
This works whether the column is a date or a timestamp, and you never have to remember whether a month has 30 or 31 days. If the column is a plain DATE, BETWEEN '2025-03-01' AND '2025-03-31' is fine, but the half-open version is a good habit.
Matching patterns with LIKE
LIKE filters text by pattern. % matches any number of characters, _ matches one. WHERE customer_name LIKE 'A%' returns orders 1 and 7 (Acme Dental). Postgres and DuckDB also have ILIKE for case-insensitive matching.
Common mistakes
AND and OR without brackets (the AI mistake to watch for)
Ask an AI assistant for "completed orders from the North or South" and it can easily write the conditions in the order you said them:
SELECT order_id, customer_name, region, status
FROM orders
WHERE region = 'North' OR region = 'South'
AND status = 'completed'
ORDER BY order_id;| order_id | customer_name | region | status |
|---|---|---|---|
| 1 | Acme Dental | North | completed |
| 2 | Birch Cafe | South | completed |
| 3 | Cobalt Labs | North | cancelled |
| 7 | Acme Dental | North | completed |
A cancelled order got in. Because AND binds first, SQL read this as region = 'North' OR (region = 'South' AND status = 'completed'): every North order, plus completed South orders. The line break makes it look grouped, but SQL ignores line breaks.
How to spot it: any WHERE that contains both AND and OR without brackets deserves a second look. Then check the output: add the filtered columns (here status) to the SELECT and scan for values that should not be there.
Forgetting a filter the question implies
"What was March revenue?" usually means completed orders only. Without a status filter, the March orders above add up to 1,130. With AND status = 'completed', it is 630. Both queries run without errors. Know your business rules, such as which statuses count as revenue, and check that every one of them is in the WHERE.
Putting aggregate conditions in WHERE
WHERE SUM(amount) > 500 is an error, because WHERE runs before rows are grouped. Conditions on totals go in HAVING: see HAVING vs WHERE.
FAQ
How do I use multiple conditions in a SQL WHERE clause?
Join them with AND (all must be true) or OR (any can be true). When you mix the two, put brackets around the OR part so SQL evaluates it the way you mean.
Is BETWEEN inclusive in SQL?
Yes, both ends are included. BETWEEN 1 AND 10 includes 1 and 10. With timestamps, the end date means midnight at the start of that day, so use >= the start and < the day after the end.
What is the difference between WHERE IN and multiple ORs?
There is none in the result. WHERE region IN ('North', 'South') is the same as WHERE region = 'North' OR region = 'South', just shorter and harder to get wrong.
Can I use a column alias in WHERE?
Not in standard SQL. Postgres and SQL Server reject it because WHERE runs before SELECT, where the alias is created. DuckDB allows it. The full explanation is in SQL order of execution.
Practise this on a realistic dataset
Solve it yourself on one company's data, with instant feedback that points at what's wrong without giving away the answer.
Try “Only the rows you want” →