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:
SELECT name, category
FROM products
WHERE category IN ('Drinks', 'Snacks', 'Bread');
SELECT * FROM orders
WHERE id IN (2, 5, 9);
NOT IN
SELECT name, barangay
FROM customers
WHERE barangay NOT IN ('Poblacion', 'San Isidro');
BETWEEN: a range
BETWEEN low AND high includes both ends:
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
SELECT student_id, score
FROM grades
WHERE subject = 'Math' AND score BETWEEN 85 AND 89;
Date ranges
SELECT id, order_date, status
FROM orders
WHERE order_date BETWEEN '2026-11-08' AND '2026-11-14';
NOT BETWEEN
SELECT name, price
FROM products
WHERE price NOT BETWEEN 10 AND 50
ORDER BY price;
BETWEEN with text
BETWEEN also works alphabetically:
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:
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. 🙂
By Sir Reyel · Last updated 2026-12-04