Before you start, you need
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.
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.
SELECT name, price
FROM products
WHERE price BETWEEN 50 AND 200;| name | price |
|---|---|
| Monitor 24 | 179 |
| Headphones | 69.9 |
| SSD Drive | 89 |
| Speaker | 54 |
- BETWEEN compares the price with two limits. It keeps figures equal to 50 and 200, as well as those in between.
- 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.
- If the question is about specific cities, use a list: city IN ('London', 'Bristol'). There are no cities «in between» to include.
- 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
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.
The question accepts two specific cities, not a range.
Put the values inside parentheses.
Each text value needs single quotes.
Solution explained
SELECT name, city FROM customers WHERE city IN ('London', 'Bristol');IN is a compact way of writing several equality checks joined with OR.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Check both boundaries
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.
The lower limit changes compared with the example: which products in the prepared copy are included now?
BETWEEN includes both the lower and the upper limit.
The condition is price BETWEEN 49 AND 200, with the lower limit first.
Solution explained
SELECT name, price FROM products WHERE price BETWEEN 49 AND 200;Boundary 49, Boundary 50 and Boundary 200 appear because BETWEEN includes both ends; Boundary 201 is left out.
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.