Filtering data by discrete lists or continuous ranges is a daily necessity in database development. The IN operator allows testing whether a value matches any item in a discrete list, while the BETWEEN operator tests whether a value falls within an inclusive numerical, date, or text range.
Instead of chaining cumbersome OR conditions:
-- Clunky syntax using chained OR:
SELECT * FROM students
WHERE course = 'BCA' OR course = 'MCA' OR course = 'B.Tech';
-- Clean, professional syntax using IN:
SELECT student_id, first_name, course
FROM students
WHERE course IN ('BCA', 'MCA', 'B.Tech');
You can also use NOT IN to exclude an entire set:
-- Exclude specific student IDs
SELECT student_id, first_name
FROM students
WHERE student_id NOT IN (101, 104, 109);
The BETWEEN operator filters values within an inclusive range (including both boundary endpoints):
-- Numeric Range: Find students with GPA between 3.50 and 3.90
SELECT student_id, first_name, gpa
FROM students
WHERE gpa BETWEEN 3.50 AND 3.90;
-- Date Range: Find enrollments during the first quarter of 2026
SELECT student_id, first_name, enrollment_date
FROM students
WHERE enrollment_date BETWEEN '2026-01-01' AND '2026-03-31';
BETWEEN val1 AND val2, val1 must always be less than or equal to val2. Writing BETWEEN 3.90 AND 3.50 will return zero rows.
Next, let us master the subtle nuances of SQL NULL Handling & Three-Valued Logic!
Yes! In SQL, BETWEEN is fully inclusive (WHERE gpa BETWEEN 3.5 AND 4.0 is mathematically identical to WHERE gpa >= 3.5 AND gpa <= 4.0).
WHERE course IN ('BCA', 'MCA', 'B.Tech') is more readable, concise, maintainable, and allows the database query optimizer to perform fast binary search lookups.
A dangerous SQL trap! If a NOT IN list contains a NULL (e.g., WHERE id NOT IN (1, 2, NULL)), the entire expression evaluates to UNKNOWN, returning zero rows! Always filter out NULLs when using NOT IN.
Proceed to the next class in sequence to build production mastery of relational database architecture.
Continue to Class 25: NULL Handling → Previous: ← LIKE & Wildcards | Full Syllabus
Filtering rows using range and set membership operators.
Mathematical equivalence between BETWEEN, >=, <=, and OR.
Using IN with nested dynamic subqueries.
Querying date intervals with BETWEEN and DATE_ADD.