SQL IN and BETWEEN
What you will learn
- How
INtests a value against a list - How
NOT INworks, and its trap withNULL - How
BETWEENtests a range, and that it includes both ends - How to use both with numbers, text and dates
This lesson runs on the sample database you chose in lesson 1.1 (use the Example data picker at the top). Edit any query and press Run. Reset data puts the original rows back.
1. IN: one of these values
Lesson 2.2 used several ORs to ask for "this or that". IN does the same with a list in brackets and is easier to read:
SELECT customer_id, first_name, city FROM customers
WHERE city IN ('Leeds', 'Cairo', 'Lima') ORDER BY customer_id LIMIT 6;
customer_id first_name city
----------- ---------- -----
1 Vik Leeds
2 Wanda Leeds
3 Dana Cairo
6 Uma Leeds
7 Ugo Leeds
18 Vera Lima
(6 rows)
SELECT account_id, status FROM accounts
WHERE status IN ('frozen', 'closed') ORDER BY account_id LIMIT 6;
account_id status
---------- ------
4 closed
7 closed
17 frozen
18 frozen
51 closed
88 frozen
(6 rows)
SELECT student_id, first_name, enrolled_year FROM students
WHERE enrolled_year IN (2023, 2026) ORDER BY student_id LIMIT 6;
student_id first_name enrolled_year
---------- ---------- -------------
1 Vera 2026
4 Quinn 2023
6 Nadia 2026
7 Jin 2023
9 Sven 2026
11 Olga 2023
(6 rows)
x IN (a, b, c) means x = a OR x = b OR x = c. Text values go in single quotes, numbers without.
2. NOT IN: none of these
NOT IN keeps the rows that are not in the list:
SELECT status, COUNT(*) AS orders FROM orders
WHERE status NOT IN ('delivered', 'cancelled') GROUP BY status ORDER BY status;
status orders
------- ------
paid 186
pending 135
shipped 231
(3 rows)
SELECT kind, COUNT(*) AS rows_found FROM transactions
WHERE kind NOT IN ('deposit', 'withdrawal') GROUP BY kind ORDER BY kind;
kind rows_found
------------ ----------
fee 403
interest 308
transfer_in 539
transfer_out 539
(4 rows)
SELECT enrolled_year, COUNT(*) AS students FROM students
WHERE enrolled_year NOT IN (2023, 2024) GROUP BY enrolled_year ORDER BY enrolled_year;
enrolled_year students
------------- --------
2025 86
2026 89
(2 rows)
A trap: if the list contains a NULL, NOT IN returns no rows at all, because SQL cannot say that a value is "not equal to unknown". Lesson 2.5 explains NULL. When a list comes from a subquery (chapter 7), make sure it cannot contain NULL:
SELECT 'a' NOT IN ('b', 'c') AS normal, 'a' NOT IN ('b', NULL) AS with_null_in_list;
normal with_null_in_list
------ -----------------
1 NULL
(1 row)
SELECT 'a' NOT IN ('b', 'c') AS normal, 'a' NOT IN ('b', NULL) AS with_null_in_list;
normal with_null_in_list
------ -----------------
1 NULL
(1 row)
SELECT 'a' NOT IN ('b', 'c') AS normal, 'a' NOT IN ('b', NULL) AS with_null_in_list;
normal with_null_in_list
------ -----------------
1 NULL
(1 row)
The second result is NULL, not 1: unknown, so a WHERE would not keep the row.
3. BETWEEN: a range
x BETWEEN low AND high is true when x is at least low and at most high. Both ends are included:
SELECT product_id, name, price FROM products
WHERE price BETWEEN 10 AND 20 ORDER BY price, product_id;
product_id name price
---------- -------------- -----
35 Hand weights 10.8
33 Yoga mat 12.0
41 Board game 12.0
17 Desk organiser 13.95
45 Card game 17.05
42 Kite 17.1
(6 rows)
SELECT account_id, balance_cents FROM accounts
WHERE balance_cents BETWEEN 1300 AND 5000 ORDER BY balance_cents, account_id;
account_id balance_cents
---------- -------------
330 1300
23 2700
405 4300
331 4800
(4 rows)
SELECT course_id, code, credits FROM courses
WHERE credits BETWEEN 2 AND 3 ORDER BY course_id;
course_id code credits
--------- ------- -------
2 MATH201 3
7 SCI301 3
8 LANG101 3
9 LANG102 2
10 LANG201 3
11 HUM101 3
12 HUM201 3
13 HUM301 2
14 ART101 2
15 ART201 2
16 ART202 2
17 CS101 3
(12 rows)
BETWEEN 10 AND 20 is the same as >= 10 AND <= 20. Always put the smaller value first: BETWEEN 20 AND 10 matches nothing.
4. BETWEEN with dates and text
Dates in the form YYYY-MM-DD work with BETWEEN, which is the natural way to ask for a period:
Orders placed during March 2026 (a month has up to 31 days, so end on the 31st):
SELECT order_id, order_date FROM orders
WHERE order_date BETWEEN '2026-03-01' AND '2026-03-31' ORDER BY order_date, order_id LIMIT 5;
order_id order_date
-------- ----------
1704 2026-03-01
92 2026-03-02
803 2026-03-02
816 2026-03-02
1459 2026-03-02
(5 rows)
Accounts opened in 2024:
SELECT account_id, opened_on FROM accounts
WHERE opened_on BETWEEN '2024-01-01' AND '2024-12-31' ORDER BY opened_on, account_id LIMIT 5;
account_id opened_on
---------- ----------
422 2024-01-01
80 2024-01-02
320 2024-01-02
254 2024-01-04
441 2024-01-05
(5 rows)
Students born during 2012:
SELECT student_id, first_name, birth_date FROM students
WHERE birth_date BETWEEN '2012-01-01' AND '2012-12-31' ORDER BY birth_date, student_id LIMIT 5;
student_id first_name birth_date
---------- ---------- ----------
278 Ines 2012-01-19
194 Vera 2012-01-24
258 Arjun 2012-01-27
332 Arjun 2012-02-07
288 Dana 2012-02-09
(5 rows)
A warning for times. If a column holds a date and a time (2026-03-31 14:30:00), then BETWEEN '2026-03-01' AND '2026-03-31' stops at midnight and misses most of 31 March. Use >= '2026-03-01' AND < '2026-04-01' instead; the end is then "before the next day". The dates in these sample databases have no time part.
Worked example: IN or BETWEEN?
| Question | Condition |
|---|---|
| "status is frozen or closed" | status IN ('frozen', 'closed') (separate values) |
| "price from 10 to 20" | price BETWEEN 10 AND 20 (a continuous range) |
| "year 2023 or 2026" | year IN (2023, 2026) (two separate values, not the years between) |
| "all of 2024" | date BETWEEN '2024-01-01' AND '2024-12-31' |
Use IN for a list of separate values and BETWEEN for a range. The columns are examples.
Common mistakes
- Smaller and larger in the wrong order in
BETWEEN. - Forgetting that
BETWEENincludes both ends. NOT INwith a list that holdsNULL: it returns nothing.BETWEENon a date-and-time column with only dates at the end.
Key takeaways
IN (...)is a short way to write severalORs;NOT INis the opposite.BETWEEN low AND highincludes both ends and needs the smaller value first.BETWEENworks for numbers, text andYYYY-MM-DDdates.- A
NULLinside aNOT INlist makes the result empty.
Practice
1. Rewrite WHERE city = 'Leeds' OR city = 'Lima' with IN.
Show answer
WHERE city IN ('Leeds', 'Lima')
2. Does price BETWEEN 10 AND 20 include a price of exactly 20?
Show answer
Yes. Both ends are included.
3. What does x BETWEEN 20 AND 10 return?
Show answer
No rows. The smaller value must come first.
4. Write a condition for "orders placed in 2025".
Show answer
WHERE order_date BETWEEN '2025-01-01' AND '2025-12-31'
5. Why can NOT IN return nothing at all?
Show answer
If the list contains NULL, the comparison is unknown for every row, so no row is kept.
6. When should you use IN and when BETWEEN?
Show answer
IN for a list of separate values, BETWEEN for a continuous range.