BBampaa

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 →
About 5–10 minutes · no sign-up needed

Example

Keep values in a range

SELECT *
FROM orders
WHERE revenue BETWEEN 40 AND 160

Result:

order_idcustomer_idrevenueorder_date
O2002C10245.02024-03-10
O2003C10378.992024-03-18
O2004C104150.02024-03-25
O2006C10640.02024-03-12

Step by step

  1. Keeps values from 40 to 160.
    SELECT * FROM orders WHERE revenue BETWEEN 40 AND 160
  2. 40 and 160 themselves are kept too.
    SELECT * FROM orders WHERE revenue BETWEEN 40 AND 160
  3. Works for dates as well, in quotes.
    SELECT * FROM orders WHERE revenue BETWEEN 40 AND 160

Practice

Try each one in your head first, then open the answer.

1. Using orders, find the orders worth between 100 and 200.
SELECT * FROM orders WHERE revenue BETWEEN 100 AND 200
2. Using products, find the products priced between 20 and 100.
SELECT * FROM products WHERE price BETWEEN 20 AND 100
3. 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 100

Learn 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 →
About 5–10 minutes · no sign-up needed