IN and BETWEEN

Updated · 8 min read
easyPostgreSQL 18sqlwhereinbetweennull

IN and BETWEEN are shortcuts for common conditions. state IN ('CA', 'TX') is a tidier way to write state = 'CA' OR state = 'TX', and fee_usd BETWEEN 400 AND 500 replaces fee_usd >= 400 AND fee_usd <= 500. Both are easy to use, and each hides one trap worth knowing: NOT IN with a NULL in the list, and BETWEEN with values that include a time of day.

In One Minute

  • column IN (a, b, c) is true when the column equals any value in the list.
  • column BETWEEN low AND high includes both ends: >= low AND <= high.
  • NOT IN returns no rows at all if the list contains a NULL.
  • For dates with times, BETWEEN '2025-12-01' AND '2025-12-31' misses times later on December 31; use >= and < instead.
  • BETWEEN needs the smaller value first; BETWEEN 500 AND 400 matches nothing.

IN: Matching a List

SQL
WHERE column IN (value1, value2, ...)
WHERE column NOT IN (value1, value2, ...)

IN checks whether a value is one of several. Courses 1, 3 and 5:

SQL▶ Try it
SELECT course_id, title
FROM courses
WHERE course_id IN (1, 3, 5)
ORDER BY course_id;
Output
 course_id |       title
-----------+-------------------
         1 | Java Basics
         3 | Java Collections
         5 | JavaScript Basics
(3 rows)

That's the same as course_id = 1 OR course_id = 3 OR course_id = 5, but shorter and easier to change. IN works with text too:

SQL▶ Try it
SELECT first_name, state
FROM students
WHERE state IN ('CA', 'TX')
ORDER BY first_name;
Output
 first_name | state
------------+-------
 Advitha    | CA
 Navika     | TX
 Srihitha   | CA
(3 rows)

NOT IN keeps the rows that match none of the values:

SQL▶ Try it
SELECT course_id, title
FROM courses
WHERE course_id NOT IN (1, 3, 5)
ORDER BY course_id;
Output
 course_id |   title
-----------+------------
         2 | Java OOP
         4 | SQL Basics
         6 | Git Basics
(3 rows)
IN keeps a row when its value is anywhere in the list

The NOT IN Trap

If the list after NOT IN contains a NULL, the condition is never true, so no rows come back. Here NULL sneaks into the list:

SQL▶ Try it
SELECT course_id, title
FROM courses
WHERE course_id NOT IN (1, NULL)
ORDER BY course_id;
Output
 course_id | title
-----------+-------
(0 rows)

Zero rows, though five courses aren't course 1. The reason is in how NOT IN works: course_id NOT IN (1, NULL) means course_id <> 1 AND course_id <> NULL. The second comparison is unknown for every row, and true AND unknown is unknown, so no row passes. You'd rarely type NULL into a list by hand, but lists produced by a subquery can easily contain one, which is why Subqueries and EXISTS come back to this.

One unknown comparison makes the whole AND unknown for every row

The same idea explains why IN and NOT IN skip rows whose own value is NULL. Payment 5 has no method, and it appears in neither of these:

SQL▶ Try it
SELECT payment_id, method
FROM payments
WHERE method NOT IN ('card')
ORDER BY payment_id;
Output
 payment_id | method
------------+--------
          3 | check
          7 | check
(2 rows)

BETWEEN: Matching a Range

SQL
WHERE column BETWEEN low AND high
WHERE column NOT BETWEEN low AND high

BETWEEN includes both ends. Courses from $400 to $500:

SQL▶ Try it
SELECT title, fee_usd
FROM courses
WHERE fee_usd BETWEEN 400 AND 500
ORDER BY fee_usd, title;
Output
       title       | fee_usd
-------------------+---------
 SQL Basics        |  400.00
 Java Basics       |  450.00
 JavaScript Basics |  450.00
 Java OOP          |  500.00
(4 rows)

SQL Basics ($400.00) and Java OOP ($500.00) are both included, because BETWEEN 400 AND 500 means >= 400 AND <= 500. NOT BETWEEN keeps everything outside, and leaves out both ends:

SQL▶ Try it
SELECT title, fee_usd
FROM courses
WHERE fee_usd NOT BETWEEN 400 AND 500
ORDER BY fee_usd, title;
Output
      title       | fee_usd
------------------+---------
 Git Basics       |  250.00
 Java Collections |  550.00
(2 rows)

The smaller value must come first. BETWEEN 500 AND 400 means >= 500 AND <= 400, which no number can be, so it matches nothing, without an error. (PostgreSQL's BETWEEN SYMMETRIC accepts either order; it's PostgreSQL only.)

400 and 500 are inside the range; NOT BETWEEN keeps only what's outside

BETWEEN with Dates and Times

With date values, BETWEEN works as you'd expect. December payments:

SQL▶ Try it
SELECT payment_id, paid_on
FROM payments
WHERE paid_on BETWEEN '2025-12-01' AND '2025-12-31'
ORDER BY paid_on;
Output
 payment_id |  paid_on
------------+------------
          3 | 2025-12-03
          7 | 2025-12-19
          4 | 2025-12-29
(3 rows)

With a timestamp, a value that includes a time of day, the same range misses most of the last day. '2025-12-31' as a timestamp means midnight at the start of December 31, so a payment at 3:00 PM that day is after the end of the range:

SQL▶ Try it
SELECT '2025-12-31 15:00'::timestamp BETWEEN '2025-12-01' AND '2025-12-31' AS with_between,
       '2025-12-31 15:00'::timestamp >= '2025-12-01'
         AND '2025-12-31 15:00'::timestamp < '2026-01-01' AS half_open;
Output
 with_between | half_open
--------------+-----------
 f            | t
(1 row)

(::timestamp turns the text into a date and time, so you can test this without a timestamp column; the Fee Tracker's columns are plain dates.) The safe pattern is a half-open range: >= the first day and < the day after the last. It works for dates and timestamps alike.

Common Mistakes

NOT IN with a list that can contain NULL. It returns nothing. Make sure the list has no NULL, or use NOT EXISTS, covered later.

Putting the larger value first in BETWEEN. BETWEEN 500 AND 400 matches no rows.

Forgetting that BETWEEN includes both ends. "Between 400 and 500" includes 400 and 500. If you mean "more than 400", write > 400.

BETWEEN on timestamps for a whole day or month. It misses times after midnight on the last day. Use >= and <.

Practice

Challenge: Find the payments made in November or December 2025 by card or check. Show payment_id, paid_on and method, sorted by paid_on. Use a half-open date range and IN.

Show solution
SQL▶ Try it
SELECT payment_id, paid_on, method
FROM payments
WHERE paid_on >= '2025-11-01' AND paid_on < '2026-01-01'
  AND method IN ('card', 'check')
ORDER BY paid_on;
Output
 payment_id |  paid_on   | method
------------+------------+--------
          6 | 2025-11-14 | card
          3 | 2025-12-03 | check
          7 | 2025-12-19 | check
          4 | 2025-12-29 | card
(4 rows)

Payments 6, 3, 7 and 4 were paid in those two months by card or check. Payment 2 was paid in October, and payment 5 in January with no method recorded.

Quick check: IN and BETWEEN

3 questions
1Which courses does WHERE fee_usd BETWEEN 400 AND 500 include?
2What does WHERE course_id NOT IN (1, NULL) return?
3Which condition safely covers all of December 2025 for a timestamp column?

Key Takeaways

You want toWrite
Any of several valuesWHERE state IN ('CA', 'TX')
None of them (no NULLs in the list)WHERE course_id NOT IN (1, 3, 5)
A range, both ends includedWHERE fee_usd BETWEEN 400 AND 500
A month, safelyWHERE paid_on >= '2025-12-01' AND paid_on < '2026-01-01'

Was this tutorial helpful?

Lesson 18 of 115