SQL IN and BETWEEN: filter by a list or a range
IN checks whether a value is in a list, BETWEEN whether it falls inside a range. They turn "Paper, Writing or Filing", "from 129 to 279" and "Q2" into short, readable conditions.
In short: WHERE category IN ('Paper', 'Filing') keeps every row whose value is in the list. NOT IN keeps all the others. WHERE price BETWEEN 129 AND 279 keeps everything from 129 up to 279, with both limits included. With dates stored as text: order_date BETWEEN '2026-04-01' AND '2026-06-30'.
Before you start: combining conditions with AND and OR (SQL AND and OR).
Try it now
Three categories, one list. The products table is shown above the editor.
Task The warehouse is counting small items: everything in the categories Paper, Writing and Filing. Show name, category and price of these products from the products table. Use IN with a list.
products 12 rows, 5 columns
| product_id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Copy paper, 500 sheets | Paper | 4.99 | 420 |
| 2 | Blue ballpoint pens, 10-pack | Writing | 3.49 | 310 |
| 3 | Lever arch file, gray | Filing | 2.79 | 185 |
| 4 | Comfort office chair | Furniture | 189 | 12 |
| 5 | Standing desk | Furniture | 449 | 5 |
| 6 | Black ink cartridge | Printer supplies | 24.9 | 64 |
| 7 | Yellow sticky notes, 12 pads | Writing | 6.99 | 0 |
| 8 | Mono laser printer L30 | Electronics | 219 | 8 |
| 9 | 27-inch monitor | Electronics | 279 | 15 |
| 10 | Notebook A5, ruled | Paper | 5.49 | 140 |
| 11 | Black toner XL | Printer supplies | 89.9 | 22 |
| 12 | Cross-cut shredder | Electronics | 129 | 0 |
How IN works
IN is shorthand for several comparisons joined with OR. These two queries return the same rows:
SELECT name, city FROM customers
WHERE city = 'Chicago' OR city = 'Leeds' OR city = 'Seattle';
SELECT name, city FROM customers
WHERE city IN ('Chicago', 'Leeds', 'Seattle');
| name | city |
|---|---|
| Emily Carter | Chicago |
| Raj Patel | Leeds |
| Daniel Kim | Seattle |
| Westbrook Dental Clinic | Chicago |
The list goes in parentheses, with commas between the values. Text follows the usual rules: single quotes, exact spelling. Numbers go without quotes, as in order_id IN (1003, 1012). The order of the list does not matter, and a value that matches nothing does no harm.
NOT IN: everything except the list
NOT IN turns the list around. Which customers are neither consumers nor businesses?
SELECT name, segment
FROM customers
WHERE segment NOT IN ('Consumer', 'Business');
| name | segment |
|---|---|
| Riverside County Office | Government |
| Maple Grove High School | School |
How BETWEEN works
BETWEEN tests a range from a lower to an upper limit, and both limits are part of it: salary BETWEEN 39600 AND 49200 means exactly salary >= 39600 AND salary <= 49200.
SELECT first_name, last_name, salary
FROM employees
WHERE salary BETWEEN 39600 AND 49200;
| first_name | last_name | salary |
|---|---|---|
| Julia | Reyes | 46800 |
| Marcus | Allen | 49200 |
| Sarah | Long | 39600 |
| Paul | Ward | 43200 |
Sarah Long at exactly 39600 and Marcus Allen at exactly 49200 make the list because the limits count. David Ross at 38400 does not. The smaller number always comes first; the other way round, you get nothing.
BETWEEN with dates
The practice database stores dates as text in the form YYYY-MM-DD, so BETWEEN works for date ranges too. Every order placed in February 2026:
SELECT order_id, customer_id, order_date
FROM orders
WHERE order_date BETWEEN '2026-02-01' AND '2026-02-28';
| order_id | customer_id | order_date |
|---|---|---|
| 1004 | 7 | 2026-02-03 |
| 1005 | 2 | 2026-02-11 |
| 1006 | 1 | 2026-02-25 |
Get the last day right: February 2026 has 28 days, and calendar quarters end on 31 March, 30 June, 30 September and 31 December. If your fiscal year starts elsewhere, as the UK tax year does on 6 April, set the limits to match.
Practice: a price range
The limits in this task appear in the table exactly as written. Are they included?
Task A customer asks about equipment and furniture priced from 129 to 279, both prices included. Show name and price of every matching product from the products table. Use BETWEEN.
products 12 rows, 5 columns
| product_id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Copy paper, 500 sheets | Paper | 4.99 | 420 |
| 2 | Blue ballpoint pens, 10-pack | Writing | 3.49 | 310 |
| 3 | Lever arch file, gray | Filing | 2.79 | 185 |
| 4 | Comfort office chair | Furniture | 189 | 12 |
| 5 | Standing desk | Furniture | 449 | 5 |
| 6 | Black ink cartridge | Printer supplies | 24.9 | 64 |
| 7 | Yellow sticky notes, 12 pads | Writing | 6.99 | 0 |
| 8 | Mono laser printer L30 | Electronics | 219 | 8 |
| 9 | 27-inch monitor | Electronics | 279 | 15 |
| 10 | Notebook A5, ruled | Paper | 5.49 | 140 |
| 11 | Black toner XL | Printer supplies | 89.9 | 22 |
| 12 | Cross-cut shredder | Electronics | 129 | 0 |
Practice: the quarterly report
Now everything together: a period with BETWEEN, a list to exclude with NOT IN, joined by AND.
Task For the quarterly report, accounting needs every closed order from Q2 2026 (1 April to 30 June): everything except the orders that are still open and those shipped but not yet delivered. Show order_id, order_date and status from the orders table. Use BETWEEN for the period and NOT IN for the statuses that do not belong.
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
Common mistakes
One set of quotes around the whole list
category IN ('Paper, Writing, Filing') is a list with a single value: one long text containing commas. No category is called that, the query returns no rows, and the exercise reports "The quotes wrap the whole list". Each value needs its own quotes: ('Paper', 'Writing', 'Filing').
A typo in one value
Write 'Filling' instead of 'Filing' and there is no error, just missing rows: "Your query returns only 4 rows. Wanted: 5 rows." Copy values from the table above the editor when you can.
Limits the wrong way round
price BETWEEN 279 AND 129 runs without complaint and returns nothing, because no price is at least 279 and at most 129. The exercise says you have no rows. Put the smaller limit first.
Expecting the limits to be excluded
If "between 129 and 279" should leave out the limits, you need price > 129 AND price < 279. BETWEEN always includes them. Read the task carefully: "from ... to" and "inclusive" point to BETWEEN.
Dates in local format
order_date BETWEEN '04/01/2026' AND '06/30/2026' compares text that starts with 0. Every date in 2026 starts with 2 and sorts after both limits, so nothing matches, and the exercise points out the date format. Always write dates as 'YYYY-MM-DD', as explained in WHERE.
NOT IN and empty values (a preview)
If the NOT IN list contains a NULL, the query returns no rows at all: WHERE employee_id NOT IN (3, 4, NULL) gives 0 of 18 orders. And rows where the column itself is empty also drop out of NOT IN (3, 4): you get 4 orders, and the 3 open orders with no employee are missing. The lesson NULL in SQL explains why.
IN and BETWEEN in MySQL, PostgreSQL, SQL Server and SQLite
All four databases support IN, NOT IN and BETWEEN, and in all of them BETWEEN includes both limits. The differences are in the details:
| Database | Worth knowing |
|---|---|
| MySQL | For BETWEEN with dates or times, the manual recommends converting the values with CAST to the matching type, for example a string to DATE. |
| PostgreSQL | Also has BETWEEN SYMMETRIC, where the limits may come in either order. |
| SQL Server | On date-and-time columns (datetime), a date without a time means midnight. BETWEEN '20260401' AND '20260630' then misses anything recorded on 30 June after 00:00. Microsoft also warns that IN lists with many thousands of values can fail; put such values in a table instead. |
| SQLite (these exercises) | The manual defines x BETWEEN y AND z as equivalent to x >= y AND x <= z. |
For periods on columns that include a time, this form is safer in any database: order_date >= '2026-04-01' AND order_date < '2026-07-01', meaning from the first day up to but not including the day after the period.
FAQ
How many values can an IN list hold?
Plenty for anything you type by hand. For very long lists, such as thousands of IDs, store the values in their own table and use a subquery. That comes in level 3.
Does BETWEEN work with text?
Yes, it then compares alphabetically: name BETWEEN 'A' AND 'M' includes names up to exactly "M" but not "Miller", because "Miller" sorts after "M". For first letters, LIKE is usually clearer.
Is IN different from a chain of ORs?
No, the result is the same. The PostgreSQL manual describes IN as shorthand for the OR chain, and NOT IN as shorthand for not-equal comparisons joined with AND. IN is almost always easier to read.
Can I combine IN and BETWEEN?
Yes, with AND and OR like any other condition, for example category IN ('Furniture', 'Electronics') AND price BETWEEN 100 AND 300.