Tutorials › SQL Tutorial › SQL IN and BETWEEN

SQL IN and BETWEEN

8 min read · Lesson 11 of 24

Two operators make common WHERE conditions shorter and easier to read: IN checks whether a value is in a list, and BETWEEN checks whether a value lies inside a range.

The IN operator

Suppose you want customers in Uganda, Tanzania or Rwanda. With OR you would write:

WHERE country = 'Uganda' OR country = 'Tanzania' OR country = 'Rwanda'

IN says the same thing in one go:

Example SQL
SELECT name, city, country
FROM customers
WHERE country IN ('Uganda', 'Tanzania', 'Rwanda');
Try it Yourself »

The list goes in brackets, with items separated by commas. It works with numbers too. Look up three specific orders:

Example SQL
SELECT order_id, customer_id, status
FROM orders
WHERE order_id IN (101, 105, 110);
Try it Yourself »

NOT IN

NOT IN keeps rows whose value is not in the list. Orders that are neither delivered nor cancelled, in other words still in progress:

Example SQL
SELECT order_id, order_date, status
FROM orders
WHERE status NOT IN ('delivered', 'cancelled');
Try it Yourself »
Warning: Be careful with NOT IN when the list could contain NULL. x NOT IN (1, 2, NULL) is never true, because SQL cannot be sure x is different from the unknown value. This catches people out when the list comes from a subquery. See Subqueries.

IN with a subquery

The list does not have to be typed by hand. It can come from another query, which is one of the most useful patterns in SQL. Customers who have at least one order:

Example SQL
SELECT name, city
FROM customers
WHERE customer_id IN (SELECT customer_id FROM orders);
Try it Yourself »

The inner query produces a list of customer IDs, and the outer query uses it like any other list. This is covered fully in Subqueries.

The BETWEEN operator

BETWEEN selects values within a range. Both ends are included. Products priced from KSh 2,000 to KSh 6,000:

Example SQL
SELECT name, price
FROM products
WHERE price BETWEEN 2000 AND 6000
ORDER BY price;
Try it Yourself »

That is exactly the same as price >= 2000 AND price <= 6000.

Always put the smaller value first. BETWEEN 6000 AND 2000 matches nothing, because no number is both at least 6000 and at most 2000.

BETWEEN with dates

Because the Skope Shop dates are stored as YYYY-MM-DD text, BETWEEN works for date ranges. Orders placed in April 2024:

Example SQL
SELECT order_id, order_date, status
FROM orders
WHERE order_date BETWEEN '2024-04-01' AND '2024-04-30';
Try it Yourself »
Tip: If a column stores a date and time (like 2024-04-30 15:20:00), then BETWEEN '2024-04-01' AND '2024-04-30' misses everything after midnight on the 30th. For date-time columns, use >= '2024-04-01' AND < '2024-05-01' instead.

BETWEEN with text

BETWEEN also works on text, using alphabetical order. Customers whose names fall alphabetically between "C" and "K":

Example SQL
SELECT name
FROM customers
WHERE name BETWEEN 'C' AND 'K'
ORDER BY name;
Try it Yourself »

Notice that Kampala Digital and Kigali Studio are not included. "Kampala Digital" comes after "K" in alphabetical order, just as "Kenya" comes after "K" in a dictionary. Text ranges are easy to get wrong, so use them carefully.

NOT BETWEEN

NOT BETWEEN gives everything outside the range. Products that are either very cheap or quite expensive:

Example SQL
SELECT name, price
FROM products
WHERE price NOT BETWEEN 2000 AND 10000
ORDER BY price;
Try it Yourself »

Combining them

IN and BETWEEN are ordinary conditions, so you can combine them with AND and OR:

Example SQL
SELECT name, category, price
FROM products
WHERE category IN ('Audio', 'Storage')
  AND price BETWEEN 1000 AND 10000;
Try it Yourself »
Exercise

Show products in the Office or Video categories.

SELECT name FROM products WHERE category  ('Office', 'Video');
Exercise

Show orders placed between 1 May and 31 May 2024.

SELECT order_id FROM orders WHERE order_date  '2024-05-01'  '2024-05-31';
Sign in or create a free account to track progress, earn XP, badges and a certificate.