IN, BETWEEN
& LIKE
Master SQL's most powerful filtering operators to query data with precision: match lists, define ranges, and find patterns.
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
How They Filter Rows
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.
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.
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.
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.
WHERE status NOT IN ('Cancelled', 'Returned');
How to Apply These Operators
Example 1: IN with a Students Table
Task: Find all students in the Math, Science, or Art departments.
FROM students
WHERE department IN ('Math', 'Science', 'Art');
| name | department |
|---|---|
| Alice | Math |
| Bob | Science |
| Clara | Art |
| David | Math |
Example 2: BETWEEN with Dates
Task: Find all orders placed between January 1 and March 31, 2024.
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.
FROM customers
WHERE email LIKE '%@gmail.com';
| name | |
|---|---|
| Eve | eve.jones@gmail.com |
| Frank | frank99@gmail.com |
| Grace | grace.h@gmail.com |
Combining All Three Operators
You can use AND / OR to combine multiple operators in one query for precise, complex filters.
FROM products
WHERE category IN ('Electronics', 'Books')
AND price BETWEEN 20 AND 100
AND name LIKE '%Pro%';
Common Mistakes
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?