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_idnamecategorypricestock
1Copy paper, 500 sheetsPaper4.99420
2Blue ballpoint pens, 10-packWriting3.49310
3Lever arch file, grayFiling2.79185
4Comfort office chairFurniture18912
5Standing deskFurniture4495
6Black ink cartridgePrinter supplies24.964
7Yellow sticky notes, 12 padsWriting6.990
8Mono laser printer L30Electronics2198
927-inch monitorElectronics27915
10Notebook A5, ruledPaper5.49140
11Black toner XLPrinter supplies89.922
12Cross-cut shredderElectronics1290

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');
namecity
Emily CarterChicago
Raj PatelLeeds
Daniel KimSeattle
Westbrook Dental ClinicChicago

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');
namesegment
Riverside County OfficeGovernment
Maple Grove High SchoolSchool

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_namelast_namesalary
JuliaReyes46800
MarcusAllen49200
SarahLong39600
PaulWard43200

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_idcustomer_idorder_date
100472026-02-03
100522026-02-11
100612026-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_idnamecategorypricestock
1Copy paper, 500 sheetsPaper4.99420
2Blue ballpoint pens, 10-packWriting3.49310
3Lever arch file, grayFiling2.79185
4Comfort office chairFurniture18912
5Standing deskFurniture4495
6Black ink cartridgePrinter supplies24.964
7Yellow sticky notes, 12 padsWriting6.990
8Mono laser printer L30Electronics2198
927-inch monitorElectronics27915
10Notebook A5, ruledPaper5.49140
11Black toner XLPrinter supplies89.922
12Cross-cut shredderElectronics1290

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_idcustomer_idorder_datestatusemployee_id
100112026-01-08delivered3
100242026-01-12delivered4
100332026-01-20delivered3
100472026-02-03delivered4
100522026-02-11cancelled8
100612026-02-25delivered3
1007102026-03-04delivered2
100852026-03-17delivered4
100982026-03-30delivered3
101062026-04-07delivered8
1011122026-04-22delivered4
101232026-05-06delivered3
101392026-05-19cancelled8
101472026-06-02shipped4
101512026-06-10shipped3
1016112026-06-12openNULL
101742026-06-13openNULL
1018102026-06-14openNULL

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:

DatabaseWorth knowing
MySQLFor BETWEEN with dates or times, the manual recommends converting the values with CAST to the matching type, for example a string to DATE.
PostgreSQLAlso has BETWEEN SYMMETRIC, where the limits may come in either order.
SQL ServerOn 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.

Up next: LIKE

So far a text value has to match exactly. LIKE searches for parts of it: every customer with "LLP" in the name, or every product that starts with "Black".

Practice LIKE

Write to us

Choose language

Select your preferred language