TetSharing
SQL tutorial · 14 chapters
  1. Get started
  2. SELECT columns
  3. WHERE filters
  4. AND, OR, NOT, IN, BETWEEN
  5. ORDER BY and LIMIT
  6. LIKE patterns
  7. NULL values
  8. COUNT, SUM, AVG, MIN, MAX
  9. GROUP BY and HAVING
  10. INNER JOIN
  11. LEFT JOIN
  12. Subqueries
  13. INSERT, UPDATE, DELETE
  14. CREATE TABLE and constraints

Final SQL quiz
Cheat sheet: sql cheat sheet

Chapter 4 of 14 · By Ethan Cole · Updated November 21, 2026

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 b keeps a row only if both conditions are true.
  • a OR b keeps it if at least one is true.
  • NOT a reverses 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.

SQLite · editable

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 AND and OR without parentheses.
  • Writing price BETWEEN 35 AND 20; the smaller value must come first, or no rows match.
  • Using NOT IN with a list that contains NULL, which makes the condition never true. The next chapters explain why NULL behaves this way.

Summary

  • AND requires every condition, OR any one, NOT reverses.
  • AND is evaluated before OR; use parentheses when mixing them.
  • IN (...) matches a list; BETWEEN a AND b is an inclusive range.

Quiz: AND, OR, NOT, IN, BETWEEN

1. Which rows does WHERE price BETWEEN 10 AND 20 include?

BETWEEN is inclusive at both ends.

2. WHERE a = 1 OR b = 2 AND c = 3 is evaluated as:

AND binds more tightly than OR.

3. Which is equivalent to city IN ('Austin', 'Boston')?

IN is shorthand for several equality tests joined by OR.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.