IN and BETWEEN
- SQL course
- Beginner
- Lesson 10/115
- 4 min
Filter with a list using IN and a range using BETWEEN.
IN and BETWEEN
IN filters values inside a list; BETWEEN filters values inside a range. IN is shorter than many = and OR.
SELECT name FROM products
WHERE price BETWEEN 10 AND 50;BETWEEN includes both endpoints; 10 and 50 are in the result.
name is selected from products. WHERE keeps only the rows where price BETWEEN 10 AND 50 holds.
IN and BETWEEN — details
IN checks whether a value equals any value in a list. The list goes in parentheses, values separated by commas. It is cleaner than a long OR chain.
BETWEEN is for ranges: WHERE price BETWEEN 10 AND 50 includes both 10 and 50.
Practice IN and BETWEEN
Select name and price from products where price is between 10 and 50.
Starter code
-- filter range with BETWEEN
SELECT name, price
FROM products
WHERESample solution
SELECT name, price
FROM products
WHERE price BETWEEN 10 AND 50Expected output
name price Defter 12 Çanta 25 Kalem 10
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Does BETWEEN include the endpoints?
- No, only the lower bound
- Yes, both bounds are included Correct answer
- Only the middle value
- No, only the upper bound
Explanation: BETWEEN includes both endpoints.
2 IN list
SELECT name FROM customers
WHERE city____('İstanbul', 'İzmir');
SELECT name FROM customers
WHERE city IN ('İstanbul', 'İzmir');
Explanation: IN compares a value with the options in a list; it is shorter than several ORs.
3 IN and BETWEEN: match the terms
| Term | Meaning |
|---|---|
IN |
list |
BETWEEN |
range |
LIKE |
pattern |
AND |
combine |