Logical & Range Operators (AND, OR, NOT, BETWEEN, IN)
Combine predicates with AND/OR/NOT and express ranges with BETWEEN and IN lists.
In plain words
AND means both must be true, OR means at least one, NOT flips it. Use brackets so the computer reads your rule the way you mean it.
Think of it like…
A shop discount rule: "members AND (spending over ₹500 OR buying on a Sunday)". Without the brackets, it reads as "(members AND spending over ₹500) OR anyone on a Sunday", which is a very different, much more expensive rule.
Where you’ve seen it
Paid or shipped orders from a premium customer: WHERE customer_tier = 'premium' AND (status = 'paid' OR status = 'shipped'), or more simply WHERE customer_tier = 'premium' AND status IN ('paid', 'shipped'). Dropping the parentheses would include every shipped order from any customer.
Words you’ll hear
- Precedence
- NOT is evaluated first, then AND, then OR.
- BETWEEN
- Inclusive range: x >= low AND x <= high.
- IN
- Shorthand for several = conditions joined by OR.
- NOT IN trap
- x NOT IN (…, NULL) is never true.
Visual diagram
Theory & example queries
AND, OR, NOT and precedence
Conditions combine with AND (both true), OR (at least one true) and NOT (the opposite). SQL evaluates NOT first, then AND, then OR, just as multiplication comes before addition. So a AND b OR c means (a AND b) OR c. When a condition mixes AND and OR, add parentheses to state the grouping you mean, even when it happens to match the default.
NULLs make the logic three-valued: NOT (x = 1) is UNKNOWN when x is NULL, so negating a condition doesn't bring NULL rows back. Rewriting with De Morgan's laws helps clarity: NOT (a OR b) equals NOT a AND NOT b.
| id | customer_id | status | amount | coupon_code |
|---|---|---|---|---|
| 101 | 1 | paid | 249 | NULL |
| 102 | 2 | paid | 120 | WELCOME10 |
| 103 | 1 | shipped | 89.5 | NULL |
SELECT idFROM ordersWHERE customer_tier = 'premium' AND ( status = 'paid' OR status = 'shipped' );PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Representative matching orders3 sample rows
| id |
|---|
| 501 |
| 518 |
| 642 |
Note: Assumes these premium orders are paid or shipped. Other premium statuses and non-premium orders are excluded.
Key takeaway: AND before OR; parenthesise every mix.
BETWEEN and IN, and their traps
BETWEEN 100 AND 500 includes both 100 and 500, which suits integers and dates. On timestamps, BETWEEN '2026-11-01' AND '2026-11-30' stops at midnight on the 30th; use >= '2026-11-01' AND < '2026-12-01' instead. BETWEEN with the bounds reversed (BETWEEN 500 AND 100) returns nothing.
IN is cleaner than a chain of ORs on one column and is usually planned the same way. NOT IN fails badly with NULLs: if the list or subquery contains a NULL, x NOT IN (…) is never true and returns no rows. Use NOT EXISTS for subqueries. For long lists from the application, pass an array (= ANY($1) in PostgreSQL) or join to a VALUES list rather than building thousands of literals.
| id | name | price | category | stock |
|---|---|---|---|---|
| 42 | SQL Notebook | 49.5 | books | 18 |
| 84 | Mechanical Keyboard | 150 | hardware | 5 |
| 11 | Query Mug | 12.99 | gifts | 33 |
SELECT *FROM productsWHERE id = ANY ($1::BIGINT[]);PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Representative matching products3 sample rows
| id | name | price |
|---|---|---|
| 11 | SQL Handbook | 999 |
| 42 | Mechanical Keyboard | 2999 |
| 77 | USB-C Hub | 1799 |
Note: Assumes an illustrative products table with these columns and all three ids present; result order is unspecified without ORDER BY.
with $1 = '{11, 42, 77}' instead of building IN (11, 42, 77) as text.
Key takeaway: BETWEEN is inclusive; NOT IN breaks on NULL; pass long lists as arrays.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Write the rule in plain words
Spell out the logic with explicit grouping: "premium customers, and status paid or shipped".
premium AND (paid OR shipped)
When to use it & trade-offs
What it is
AND requires both conditions, OR either one, and NOT reverses a condition. BETWEEN a AND b is shorthand for >= a AND <= b, inclusive at both ends. IN (…) checks membership in a list: status IN ('paid', 'shipped'). AND is evaluated before OR, so mixing them without parentheses often means something different from what you intended.
Why it matters: The AND/OR precedence bug returns wrong rows without any error, which makes it one of the most common logic mistakes in reports and access checks. BETWEEN on timestamps and NOT IN with NULLs are two more classic traps that interviewers like to test.
When to reach for it
- Combining filters on status, dates and amounts
- Membership checks with IN
- Integer and date ranges with BETWEEN
When to avoid or be careful
- Mixing AND and OR without parentheses
- BETWEEN on timestamp columns
- NOT IN against data that can contain NULL
IN vs OR chains vs = ANY(array)
Strengths: Readable; planned efficiently.
Watch out: Building long lists as SQL text is clumsy and risky.
Strengths: Expresses any combination.
Watch out: Easy to get precedence wrong; verbose for one column.
Strengths: One parameter, safe and plan-friendly.
Watch out: PostgreSQL syntax; other databases use table-valued parameters or temp tables.
Interview signals & numbers to know
When this topic comes up
- “The report includes rows it shouldn't”
- “Orders that are paid or shipped”
- “Between two dates”
- NOT IN returns nothing
- Access-control conditions
Benchmarks & rules of thumb
- NOT > AND > OR
- evaluation order
- Both ends
- included by BETWEEN
- 1 NULL
- makes NOT IN return no rows
Common mistakes & follow-ups
Common interview mistakes
- Writing a AND b OR c and getting every row where c is true.
- Using BETWEEN on a timestamp and missing most of the end date.
- Using NOT IN (SELECT …) when the subquery can return NULL.
Likely interviewer follow-ups
- How does SQL evaluate a AND b OR c?
- Is BETWEEN inclusive or exclusive?
- Why can NOT IN return no rows at all?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Logical & Range Operators (AND, OR, NOT, BETWEEN, IN) with hands-on SQL practice and interview Q&A.