IN and BETWEEN
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 highincludes both ends:>= low AND <= high.NOT INreturns no rows at all if the list contains aNULL.- For dates with times,
BETWEEN '2025-12-01' AND '2025-12-31'misses times later on December 31; use>=and<instead. BETWEENneeds the smaller value first;BETWEEN 500 AND 400matches nothing.
IN: Matching a List
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:
SELECT course_id, title
FROM courses
WHERE course_id IN (1, 3, 5)
ORDER BY course_id; 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:
SELECT first_name, state
FROM students
WHERE state IN ('CA', 'TX')
ORDER BY first_name; first_name | state
------------+-------
Advitha | CA
Navika | TX
Srihitha | CA
(3 rows)NOT IN keeps the rows that match none of the values:
SELECT course_id, title
FROM courses
WHERE course_id NOT IN (1, 3, 5)
ORDER BY course_id; course_id | title
-----------+------------
2 | Java OOP
4 | SQL Basics
6 | Git Basics
(3 rows)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:
SELECT course_id, title
FROM courses
WHERE course_id NOT IN (1, NULL)
ORDER BY course_id; 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.
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:
SELECT payment_id, method
FROM payments
WHERE method NOT IN ('card')
ORDER BY payment_id; payment_id | method
------------+--------
3 | check
7 | check
(2 rows)BETWEEN: Matching a Range
WHERE column BETWEEN low AND high
WHERE column NOT BETWEEN low AND highBETWEEN includes both ends. Courses from $400 to $500:
SELECT title, fee_usd
FROM courses
WHERE fee_usd BETWEEN 400 AND 500
ORDER BY fee_usd, title; 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:
SELECT title, fee_usd
FROM courses
WHERE fee_usd NOT BETWEEN 400 AND 500
ORDER BY fee_usd, title; 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.)
BETWEEN with Dates and Times
With date values, BETWEEN works as you'd expect. December payments:
SELECT payment_id, paid_on
FROM payments
WHERE paid_on BETWEEN '2025-12-01' AND '2025-12-31'
ORDER BY paid_on; 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:
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; 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
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; 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.
Key Takeaways
| You want to | Write |
|---|---|
| Any of several values | WHERE state IN ('CA', 'TX') |
| None of them (no NULLs in the list) | WHERE course_id NOT IN (1, 3, 5) |
| A range, both ends included | WHERE fee_usd BETWEEN 400 AND 500 |
| A month, safely | WHERE paid_on >= '2025-12-01' AND paid_on < '2026-01-01' |
Was this tutorial helpful?