Aral Code

Filtering & Sorting Lesson 9 of 21

SQL IN and BETWEEN

Match a value against a list with IN, check ranges of numbers and dates with BETWEEN, and reverse them with NOT IN and NOT BETWEEN.

Two handy shortcuts make WHERE clauses shorter and easier to read: IN checks if a value is in a list, and BETWEEN checks if a value is inside a range.

IN: matching a list

Instead of writing many OR conditions:

WHERE category = 'Drinks' OR category = 'Snacks' OR category = 'Bread'

…use IN:

SQL Drinks, snacks and bread
SELECT name, category
FROM products
WHERE category IN ('Drinks', 'Snacks', 'Bread');
SQL Specific orders by id
SELECT * FROM orders
WHERE id IN (2, 5, 9);

NOT IN

SQL Customers outside Poblacion and San Isidro
SELECT name, barangay
FROM customers
WHERE barangay NOT IN ('Poblacion', 'San Isidro');

BETWEEN: a range

BETWEEN low AND high includes both ends:

SQL Products from ₱15 to ₱25
SELECT name, price
FROM products
WHERE price BETWEEN 15 AND 25
ORDER BY price;

This is the same as price >= 15 AND price <= 25. Notice ₱15 and ₱25 items are included.

Grade ranges

SQL 'Very Satisfactory' Math scores (85–89)
SELECT student_id, score
FROM grades
WHERE subject = 'Math' AND score BETWEEN 85 AND 89;

Date ranges

SQL Orders in the second week of November
SELECT id, order_date, status
FROM orders
WHERE order_date BETWEEN '2026-11-08' AND '2026-11-14';

NOT BETWEEN

SQL Products outside the ₱10–₱50 range
SELECT name, price
FROM products
WHERE price NOT BETWEEN 10 AND 50
ORDER BY price;

BETWEEN with text

BETWEEN also works alphabetically:

SQL Last names from A to C
SELECT last_name, first_name
FROM students
WHERE last_name BETWEEN 'A' AND 'D'
ORDER BY last_name;

We use 'D' as the upper end because a name like "Dela Cruz" is "greater than" just 'D', so it's not included, while every name starting with A, B or C is.

IN with a subquery (preview)

The list inside IN can come from another query. This finds customers who have unpaid orders:

SQL Customers with utang
SELECT name, phone
FROM customers
WHERE id IN (SELECT customer_id FROM orders WHERE status = 'utang');

This is called a subquery. It gets its own lesson later.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Is ₱25 included in price BETWEEN 15 AND 25?
2. Which is the same as id IN (1, 2, 3)?
3. Which finds products NOT in Drinks or Snacks?
4. What is a subquery?

By Sir Reyel · Last updated 2026-12-04