Module 02 · lesson 4
BETWEEN: ranges of values
For the site filter "products from $40 to $70" it's handier to set a range with a single expression rather than two conditions.
You'll need:Comparison operators(opens in a new tab)
A range in one expression
BETWEEN checks whether a value is within a range — inclusive of the bounds. For example, products with stock from 20 to 100 units:
SELECT name, stock
FROM products
WHERE stock BETWEEN 20 AND 100;
This is the same as stock >= 20 AND stock <= 100, only shorter and more readable. BETWEEN works with dates too:
WHERE created_at BETWEEN '2024-02-01' AND '2024-02-29'
The negation is NOT BETWEEN.
Your turn
The site filter needs products priced from $40 to $70 inclusive. Output their name and price.
Summary
BETWEEN a AND bsets a range including the bounds — a shorter form for paired comparisons.- Equivalent to
>= a AND <= b. - Works with numbers and dates; there's also
NOT BETWEEN.