SQL IN & BETWEEN: Range Filtering & Set Membership

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.


1. The IN Operator (Set Membership)

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);

2. The BETWEEN Operator (Continuous Ranges)

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';
Boundary Rule: In 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!

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!