01 / 22

IN, BETWEEN
& LIKE

Master SQL's most powerful filtering operators to query data with precision: match lists, define ranges, and find patterns.

SQL

Why Filtering Matters

Real databases contain millions of rows. The WHERE clause is how you narrow results to exactly what you need. But writing long chains of OR conditions or complex comparisons gets messy fast.

Meet the Three Operators

IN
Matches a value against a list of specific options
BETWEEN
Checks if a value falls within a range (inclusive)
LIKE
Searches for a pattern using wildcard characters

How They Filter Rows

ALL ROWS WHERE MATCHED ROWS IN ('A', 'B', 'C') BETWEEN 10 AND 50 LIKE '%pattern%' ✓ Only matching rows returned

The IN Operator

The IN operator lets you specify a list of values. A row matches if its column value equals any value in that list.

SELECT * FROM students
WHERE grade IN ('A', 'B', 'C');

The BETWEEN Operator

BETWEEN checks if a value falls within a range, and it is inclusive on both ends: the boundary values are included in the result.

SELECT * FROM products
WHERE price BETWEEN 10 AND 50;

The LIKE Operator

LIKE performs pattern matching on text columns. It uses two special wildcard characters to represent unknown portions of a string.

% (Percent)
Matches zero or more characters of any kind
_ (Underscore)
Matches exactly one character of any kind
SELECT * FROM customers
WHERE name LIKE 'J%'; -- starts with J

Common LIKE Patterns

Pattern Meaning Matches
'A%' Starts with 'A' Alice, Alex, Anna
'%son' Ends with 'son' Johnson, Wilson
'%an%' Contains 'an' Danny, Fran, Mango
'_r%' 'r' is the 2nd character Arthur, Bruce
'H____' Starts with 'H', 5 chars total Harry, Helen

Key Distinction: When to Use Each

Feature IN BETWEEN LIKE
Data type Any Numbers, dates, text Text (strings)
Use case Discrete list Continuous range Pattern search
Boundaries Exact matches Inclusive on both ends Wildcards
Negation NOT IN NOT BETWEEN NOT LIKE

NOT Variants: Excluding Data

Each operator has a negated form that returns the opposite result: rows that do not match the condition.

1
NOT IN: Excludes rows matching any value in the list
2
NOT BETWEEN: Excludes rows within the range (returns values outside it)
3
NOT LIKE: Excludes rows matching the pattern
SELECT * FROM orders
WHERE status NOT IN ('Cancelled', 'Returned');

How to Apply These Operators

1
Identify the question: Are you looking for specific values, a range, or a pattern?
2
Pick the operator: Discrete values → IN. Numeric/date range → BETWEEN. Text pattern → LIKE.
3
Write the WHERE clause: Place the column name first, then the operator and its argument.
4
Check boundaries: Remember BETWEEN is inclusive. For LIKE, choose % vs _ carefully.
5
Test with NOT: If you need to exclude instead, prepend NOT before the operator.

Example 1: IN with a Students Table

Task: Find all students in the Math, Science, or Art departments.

SELECT name, department
FROM students
WHERE department IN ('Math', 'Science', 'Art');
namedepartment
AliceMath
BobScience
ClaraArt
DavidMath

Example 2: BETWEEN with Dates

Task: Find all orders placed between January 1 and March 31, 2024.

SELECT order_id, order_date, total
FROM orders
WHERE order_date BETWEEN '2024-01-01'
  AND '2024-03-31';

Example 3: LIKE with Wildcards

Task: Find all customers whose email is from Gmail.

SELECT name, email
FROM customers
WHERE email LIKE '%@gmail.com';
nameemail
Eveeve.jones@gmail.com
Frankfrank99@gmail.com
Gracegrace.h@gmail.com

Combining All Three Operators

You can use AND / OR to combine multiple operators in one query for precise, complex filters.

SELECT name, price, category
FROM products
WHERE category IN ('Electronics', 'Books')
  AND price BETWEEN 20 AND 100
  AND name LIKE '%Pro%';

Common Mistakes

✕ Forgetting Quotes
String values in IN and LIKE must be wrapped in single quotes. Numbers do not need quotes.
✕ Wrong BETWEEN Order
The smaller value must come first: BETWEEN 10 AND 50, not BETWEEN 50 AND 10.
✕ Case Sensitivity
LIKE is case-sensitive in some databases (PostgreSQL). Use ILIKE or LOWER() for case-insensitive matching.

Summary Cheat Sheet

Syntax Example Returns
col IN (a, b, c) city IN ('NYC', 'LA') Rows where city is 'NYC' or 'LA'
col NOT IN (a, b) status NOT IN ('Done') Rows where status is not 'Done'
col BETWEEN x AND y age BETWEEN 18 AND 25 Rows where age is 18 to 25
col NOT BETWEEN x AND y price NOT BETWEEN 0 AND 10 Rows where price is < 0 or > 10
col LIKE 'pattern' name LIKE 'A%' Rows where name starts with 'A'
col NOT LIKE 'pattern' email NOT LIKE '%spam%' Rows where email lacks "spam"

Question 1 of 5

Which SQL query correctly returns all employees in the Sales, Marketing, or HR departments?

Question 2 of 5

What does the query WHERE age BETWEEN 18 AND 30 return?

Question 3 of 5

Which LIKE pattern matches any name that ends with "son"?

Question 4 of 5

What does the underscore (_) wildcard do in a LIKE pattern?

Question 5 of 5

Which query finds products whose price is outside the range of 100 to 500?