Lessons › SQL › Chapter 2: Filtering
SQL lesson
BETWEEN
BETWEEN keeps values inside a range, like revenue BETWEEN 40 AND 160. Both ends count, so 40 and 160 are kept too. Put the smaller number first. It works for dates as well.
Try this lesson free →Example
Keep values in a range
SELECT *
FROM orders
WHERE revenue BETWEEN 40 AND 160| order_id | customer_id | revenue | order_date |
|---|---|---|---|
| O2002 | C102 | 45.0 | 2024-03-10 |
| O2003 | C103 | 78.99 | 2024-03-18 |
| O2004 | C104 | 150.0 | 2024-03-25 |
| O2006 | C106 | 40.0 | 2024-03-12 |
Step by step
- Keeps values from 40 to 160.
SELECT * FROM orders WHERE revenue BETWEEN 40 AND 160 - 40 and 160 themselves are kept too.
SELECT * FROM orders WHERE revenue BETWEEN 40 AND 160 - Works for dates as well, in quotes.
SELECT * FROM orders WHERE revenue BETWEEN 40 AND 160
Practice
1. Using orders, find the orders worth between 100 and 200.
SELECT * FROM orders WHERE revenue BETWEEN 100 AND 2002. Using products, find the products priced between 20 and 100.
SELECT * FROM products WHERE price BETWEEN 20 AND 1003. Using customers, find the customers who signed up in January 2024: between '2024-01-01' and '2024-01-31'.
SELECT * FROM customers WHERE signup_date BETWEEN '2024-01-01' AND '2024-01-31'4. Using store_sales, find the sales made between 2024-03-02 and 2024-03-04.
SELECT * FROM store_sales WHERE sale_date BETWEEN '2024-03-02' AND '2024-03-04'5. Using returns, show the return_id and refund_amount of refunds between 50 and 100.
SELECT return_id, refund_amount FROM returns WHERE refund_amount BETWEEN 50 AND 100Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.
Try this lesson free →