CPCODELAB
SQL

IN & BETWEEN — Range and Set Filtering

6 min read

IN and BETWEEN

IN tests whether a value matches any value in a list. BETWEEN tests whether a value falls within an inclusive range. Both are syntactic sugar — they could be written with = / OR and >= / <= — but they are much easier to read.

sql
-- Students from a specific set of cities
SELECT name, city FROM students
WHERE city IN ('Mumbai', 'Delhi', 'Bengaluru');

-- Students NOT from those cities
SELECT name, city FROM students
WHERE city NOT IN ('Mumbai', 'Delhi', 'Bengaluru');

-- Courses priced between 500 and 2000 (inclusive)
SELECT title, price FROM courses
WHERE price BETWEEN 500 AND 2000;

-- Enrolments in the first half of 2024
SELECT * FROM enrolments
WHERE enrolled_at BETWEEN '2024-01-01' AND '2024-06-30';

BETWEEN is always inclusive on both ends. If you need to exclude one boundary, switch back to >= and < operators.

Ready to test yourself?
Practice SQL— quiz & coding exercises