Module 2 · Filtering data

Filter ranges and lists with BETWEEN and IN

By the end you will be able to filter by an interval or a list of values.

About 10 min

0 of 52 lessons

Before you start, you need

Combine conditions with AND, OR and NOT

Choose a range or a list

Some filters accept a range of values, such as prices from 50 to 200. Others support a specific list, such as London and Bristol. SQL has a short form for each case.

BETWEEN 50 AND 200 includes intermediate values and both ends. For the price, it is equivalent to price >= 50 AND price <= 200. A price of 50 gets in; one of 49 does not.

IN ('London', 'Bristol') accepts any of the cities in the list. Parentheses surround the values, separated by commas. Since they are text, they take single quotes.

Choose according to the question: BETWEEN for a range and IN for specific alternatives. In both cases WHERE still checks one row at a time. The practice task includes 49, 50, 200 and 201 so you can check the ends.

Minimal syntax
WHERE price BETWEEN 50 AND 200
-- or: WHERE city IN ('London', 'Bristol')

See it in action

We want products priced from 50 to 200, both ends included.

SQL
SELECT name, price
FROM products
WHERE price BETWEEN 50 AND 200;
Example result: 4 rows
nameprice
Monitor 24179
Headphones69.9
SSD Drive89
Speaker54
  1. BETWEEN compares the price with two limits. It keeps figures equal to 50 and 200, as well as those in between.
  2. The values 49 and 201 would not pass. In the challenge the lower limit changes and you will have to decide which boundaries are included, using a prepared copy of the catalogue.
  3. If the question is about specific cities, use a list: city IN ('London', 'Bristol'). There are no cities «in between» to include.
  4. Write BETWEEN's lower limit first, and put the IN list in parentheses.

Practice with help

Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.

Guided practice

Find two cities

Not started

Write a query that returns name and city for customers who live in London or Bristol. The order does not matter.

Expected result: 6 rows with the columns name, city, in that order. Row order does not matter.

View tables and data

Open this panel to see the full data.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Check both boundaries

Not started

Show name and price of the products priced between 49 and 200, both included. Use BETWEEN.

Expected result: 8 rows with columns name, price, in that order. The order of the rows does not matter.

This activity uses a copy of the data with extra edge cases. Open the full data before you predict the result.

View tables and data

Open this panel to see the full data.

Key takeaways

  • BETWEEN includes both of its ends.
  • IN accepts any of the values in a list.
  • Choose based on whether you have a continuous range or specific alternatives.

Next: Find text patterns with LIKE