SQL AND, OR, NOT, IN and BETWEEN Operators
Real filters usually have several parts: Tech or Nonfiction books, priced between 20 and 35, published in 2015 or later. SQL combines conditions with the logical operators AND, OR and NOT, plus two shortcuts, IN and BETWEEN, that make common patterns shorter.
AND, OR, NOT
a AND bkeeps a row only if both conditions are true.a OR bkeeps it if at least one is true.NOT areverses a condition.
AND binds more tightly than OR, just as multiplication binds more tightly than addition. WHERE genre = 'Tech' OR genre = 'Nonfiction' AND price > 30 is read as genre = 'Tech' OR (genre = 'Nonfiction' AND price > 30), so every Tech book passes regardless of price. Whenever you mix AND and OR, add parentheses to say what you mean.
IN: match any value in a list
genre IN ('Tech', 'Nonfiction') is shorthand for genre = 'Tech' OR genre = 'Nonfiction'. It is shorter, avoids the precedence trap, and is easy to extend. NOT IN excludes the listed values. Later, in the subqueries chapter, the list inside IN will come from another query.
BETWEEN: inclusive ranges
price BETWEEN 20 AND 35 means price >= 20 AND price <= 35. Both ends are included. That matters for dates stored as text with a time part: ordered_on BETWEEN '2025-10-01' AND '2025-10-31' would miss '2025-10-31 14:00', because that string is greater than '2025-10-31'. For such columns, use >= the start and < the day after the end.
Expected output
title | genre | price Learning SQL by Doing | Tech | 29.0 Small Data, Big Ideas | Tech | 34.99 Maps of Memory | Nonfiction | 24.5
Reading the example
The query keeps Tech and Nonfiction books (IN), priced from 20 to 35 inclusive (BETWEEN), and drops anything published before 2015 (NOT). "Desert Gardens" matches the first two conditions but was published in 2012, so NOT year < 2015 removes it. "Northern Lights Code" is Tech but costs 39.5, outside the range. Three books remain.
NOT year < 2015 is the same as year >= 2015; the positive form is usually easier to read, and the example uses NOT only to show it.
Common mistakes
- Mixing
ANDandORwithout parentheses. - Writing
price BETWEEN 35 AND 20; the smaller value must come first, or no rows match. - Using
NOT INwith a list that containsNULL, which makes the condition never true. The next chapters explain whyNULLbehaves this way.
Summary
ANDrequires every condition,ORany one,NOTreverses.ANDis evaluated beforeOR; use parentheses when mixing them.IN (...)matches a list;BETWEEN a AND bis an inclusive range.
Quiz: AND, OR, NOT, IN, BETWEEN