BETWEEN, IN, and LIKE: concise filters
Handy operators for conditions
Restorers of old Earth reconstructed books from fragments of patterns on their covers. They didn't always need the exact title. Sometimes a clue was enough: the title starts with a certain character, the book belongs to a certain section, or its date falls within a certain range.
A data archivist works in much the same way.
In WHERE, writing many conditions with AND and OR can get cumbersome. Sometimes a shorter operator expresses the same idea more clearly:
price >= 10 AND price <= 30
can be written as:
price BETWEEN 10 AND 30
And this long chain:
category = 'Books'
OR category = 'Toys'
OR category = 'Accessories'
can be replaced with:
category IN ('Books', 'Toys', 'Accessories')
And if you need to find products whose names start with the word “Coffee,” you can use a pattern:
name LIKE 'Coffee%'
This lesson focuses on three tools:
BETWEEN a AND b— the value falls within the range fromatob;IN (...)— the value is in a list;LIKE 'pattern'— the text matches a text pattern.
These operators simply give you shorter, clearer ways to express common conditions.
QUERY: A good filter is like a precise command to a reconnaissance drone: instead of “find something useful,” say “show me products from these categories, in this range, whose names begin like this.”

BETWEEN range, an IN list, and a LIKE stencil—the archive finds the matches for you.Three different ways to search
These operators have different jobs.
BETWEEN answers this question:
Is the value between two boundaries?
For example:
price BETWEEN 10 AND 30
Read it as:
price from $10.00 through $30.00, inclusive.
IN answers this question:
Is the value one of these options?
For example:
category IN ('Books', 'Toys')
Read it as:
category is Books or Toys.
LIKE answers this question:
Does the text match this pattern?
For example:
name LIKE 'Coffee%'
Read it as:
the name starts with “Coffee.”
Here's the difference:
| Operator | When to use it | Example |
|---|---|---|
BETWEEN | you need a range | price BETWEEN 10 AND 30 |
IN | you need a list of options | category IN ('Books', 'Toys') |
LIKE | you need a text pattern | name LIKE 'Coffee%' |
BETWEEN: a value between two boundaries
BETWEEN checks whether a value falls within a range.
For example, suppose you need to find products priced from $10.00 through $30.00:
SELECT name, price
FROM products
WHERE price BETWEEN 10 AND 30;
That's the same as writing:
SELECT name, price
FROM products
WHERE price >= 10
AND price <= 30;
The main detail to remember:
BETWEEN includes both boundaries.
So this condition:
price BETWEEN 10 AND 30
will return products priced at:
- $10.00
- $15.00
- $29.99
- $30.00
The value 10, meaning $10.00, is included.
The value 30, meaning $30.00, is included too.
If you need values strictly greater than $10.00 and strictly less than $30.00, BETWEEN isn't suitable. Use ordinary comparisons instead:
WHERE price > 10
AND price < 30
BETWEEN 10 AND 30 includes both boundaries: both $10.00 and $30.00 will appear in the result.products, return name and stock for products that have 60 to 90 units in stock, inclusive.Important details about BETWEEN
1. The boundaries aren't reordered automatically
This filter is valid:
WHERE price BETWEEN 10 AND 30
But this one will almost always return an empty result:
WHERE price BETWEEN 30 AND 10
That's because the database interprets it as:
WHERE price >= 30
AND price <= 10
An ordinary number can't be both greater than or equal to $30.00 and less than or equal to $10.00.
So the order of the boundaries matters: lower first, upper second.
2. There's an opposite form: NOT BETWEEN
If you need to find products outside a range, you can write:
SELECT name, price
FROM products
WHERE price NOT BETWEEN 10 AND 30;
This is similar to:
WHERE price < 10
OR price > 30
A product priced at $5.00 will be included.
A product priced at $40.00 will be included.
A product priced at $10.00 won't be included.
A product priced at $30.00 won't be included.
That's because the boundaries themselves are part of the BETWEEN range.
3. Be more careful with dates and times
BETWEEN works with dates too:
WHERE created_at BETWEEN '2024-01-01' AND '2024-01-31'
But if created_at contains both a date and a time, for example:
2024-01-31 18:45:00
this filter may accidentally leave out part of the final day.
Why? For a timestamp, '2024-01-31' often means the start of that day:
2024-01-31 00:00:00
For time periods, it's often safer to use a half-open interval:
WHERE created_at >= '2024-01-01'
AND created_at < '2024-02-01'
This includes everything from January 1 onward up to, but not including, February 1.
For now, remember this rule:
BETWEENis convenient and clear for numbers. With dates and times, check the boundaries especially carefully.
IN: a value is in a list
Use IN when you need to check a value against several possible options.
For example, suppose you need to find products only from the Books and Toys categories:
SELECT name, category, price
FROM products
WHERE category IN ('Books', 'Toys');
That's shorter and clearer than:
SELECT name, category, price
FROM products
WHERE category = 'Books'
OR category = 'Toys';
Read IN like this:
category is in the list: Books, Toys.
IN is especially useful when there are many options:
WHERE category IN ('Books', 'Toys', 'Accessories', 'Pet Food', 'Pet Houses')
Instead of a long chain of OR conditions, you get one compact filter.
users, return full_name and city for everyone whose city is one of these three: Denver, Halifax, Cancun.Important details about IN
1. IN is for lists of exact values
IN doesn't search for similar values. It checks for an exact match with one of the options.
WHERE category IN ('Books', 'Toys')
It will find the category 'Books'.
But it won't find:
Book
books
BOOKS
Electronic Books
Those are different strings.
2. NOT IN excludes values from a list
If you need to exclude several categories, you can write:
SELECT name, category, price
FROM products
WHERE category NOT IN ('Books', 'Toys');
Read it as:
show products whose category isn't in the list Books, Toys.
But NOT IN has an important trap involving NULL. We'll cover it in a separate section below.
3. IN works well with BETWEEN
For example:
Read it as:
show products in the Books and Toys categories priced from $10.00 through $30.00.
This gives you a complete practical filter: a list of categories plus a price range.
LIKE: searching with a text pattern
Use LIKE when you want to find text by a pattern rather than by an exact match.
For example, suppose you need to find products whose names start with the word “Coffee”:
SELECT name, price
FROM products
WHERE name LIKE 'Coffee%';
The % symbol means:
any number of any characters.
That includes zero characters.
So the pattern:
'Coffee%'
will find:
Coffee
Coffee Station
Coffee for Catonauts
But it won't find:
Ground Coffee
Large coffee
Set: coffee and mug
That's because the pattern requires the text to start with Coffee.
LIKE stencil: % stretches across any number of characters, including zero, while _ takes exactly one position.%: any number of characters
% is the special character you'll use most often with LIKE.
It means:
any number of characters can appear here.
Compare a few different patterns.
Starts with text
WHERE name LIKE 'Coffee%'
Finds names that start with Coffee.
Examples of matches:
Coffee
Coffee Maker
Coffee Set
Ends with text
WHERE name LIKE '%coffee'
Finds names that end with coffee.
Examples of matches:
Large coffee
Capsule coffee
Set for coffee
Contains text anywhere
WHERE name LIKE '%coffee%'
Finds names where coffee appears anywhere in the text.
Examples of matches:
coffee
Large coffee
Set for coffee and tea
Fresh coffee beans
But this pattern won't find “Coffee Station”: LIKE is case-sensitive, and C and c are different characters. We'll look at case sensitivity in more detail later in this lesson.
Another way to read these patterns:
'Coffee%'
— the text must start with “Coffee.”
'%coffee'
— the text must end with “coffee.”
'%coffee%'
— the text must contain “coffee” somewhere.
_: exactly one character
Besides %, LIKE has another special character:
_
It means:
exactly one character of any kind.
For example:
WHERE code LIKE 'A_1'
These match:
AB1
AX1
A71
But these don't:
A1
ABCD1
AA21
Why?
The pattern A_1 requires:
- first,
A - then exactly one character of any kind
- then
1
Here's another example:
WHERE name LIKE 'Cat_'
These four-character strings match:
Cats
CatX
Cat1
But these don't:
Cat
Kitten
That's because _ requires exactly one additional character.
Here's the difference:
| Pattern | Meaning |
|---|---|
'Cat%' | Cat, followed by any number of characters |
'Cat_' | Cat, followed by exactly one character |
users, return id and full_name for customers whose full_name contains the surname Smith; in the English data, the surname has the same form for everyone—Smith.Case sensitivity: LIKE, ILIKE, and LOWER
In PostgreSQL, ordinary LIKE is case-sensitive.
That means this pattern:
WHERE name LIKE 'smart%'
may not find this string:
Smartwatch
That's because s and S are different characters.
For case-insensitive searches, PostgreSQL provides ILIKE:
SELECT name, price
FROM products
WHERE name ILIKE 'smart%';
ILIKE is the case-insensitive version of LIKE.
It will find:
smartwatch
Smartwatch
SMARTWATCH
Keep in mind that ILIKE is a PostgreSQL feature, not a universal part of standard SQL.
A more portable approach is to convert the text to a single case:
SELECT name, price
FROM products
WHERE LOWER(name) LIKE 'smart%';
Here, the database first converts name to lowercase, then compares it with a lowercase pattern.
You can also write:
WHERE LOWER(name) LIKE LOWER('Smart%')
But in most cases, you can simply write the pattern in the desired case from the start:
WHERE LOWER(name) LIKE 'smart%'
How to search for literal % and _ characters
There's a complication with LIKE: % and _ are special characters.
% means any number of characters.
_ means exactly one character.
But sometimes you need to find an actual percent sign or underscore as ordinary text.
Suppose you have these products:
Discount 50%
Food 50 kg
QA_kit
QA-kit
If you write:
WHERE name LIKE '50%'
that doesn't mean “find the text 50%.”
It means:
find strings that start with
50, followed by anything.
To tell the database, “this % is a percent sign,” use ESCAPE.
For example:
SELECT name
FROM products
WHERE name LIKE '%50!%%' ESCAPE '!';
Break the pattern down like this:
'%50!%%'
- the first
%— any beginning of the string 50— literal characters!%— a literal percent sign, because!is the escape character- the final
%— any continuation of the string
This clause:
ESCAPE '!'
tells SQL:
if
!appears before%or_, treat the next character as literal text.
You can search for an underscore the same way:
SELECT name
FROM products
WHERE name LIKE 'QA!_%' ESCAPE '!';
This pattern finds strings that start with the literal text QA_.
Without escaping, the pattern:
'QA_%'
would mean:
QA, then any single character, then any continuation.
So it could match extra strings such as:
QA-kit
QA1kit
QA kit
QUERY: In a
LIKEstencil,%and_aren't paint—they're holes. If you want to find the hole itself on the blueprint, first tell the archive to treat it as an ordinary symbol.
The cost of a leading %
On a small table, you may barely notice a difference. Across millions of rows, however, different LIKE patterns can perform very differently.
Compare these two conditions:
WHERE name LIKE 'Coffee%'
and
WHERE name LIKE '%coffee'
The first condition gives the database the beginning of the string. It can work a bit like someone using a paper dictionary:
Open the section for “Coffee” and search nearby.
An index can sometimes speed up this kind of prefix search.
The second condition starts with %:
'%coffee'
That means:
anything may come before the word “coffee.”
The match could appear anywhere in the string. A regular index on the name column no longer helps in the same way because the database doesn't know what beginning to search for.
That's why queries like:
WHERE name LIKE '%coffee%'
often require checking many rows: the database has to inspect each name.
Here's the basic idea:
| Condition | What the database knows | Usually faster? |
|---|---|---|
LIKE 'Coffee%' | the prefix is known | yes, an index may help |
LIKE '%coffee' | the beginning is unknown | often slow |
LIKE '%coffee%' | the match can be anywhere | often slow |
PostgreSQL has some technical nuances: speeding up prefix LIKE searches may sometimes require an index with text_pattern_ops or a suitable locale, while searches in the middle of strings can use trigram indexes from pg_trgm.
At this stage, focus on this idea:
If a pattern starts with ordinary characters, it's easier for the database to search. If it starts with
%, the database often has to scan much more of the table.
A condition's ability to use an index is called . We'll cover it in detail in the optimization module.
Practical rule
Good:
WHERE name LIKE 'Coffee%'
Use with care:
WHERE name LIKE '%coffee%'
The first query searches using a known beginning of the string.
The second searches for a substring anywhere in the string. On a large table, that can be expensive.
LIKE '%coffee%' is still perfectly valid, and sometimes it's exactly what you need. But if this kind of search runs frequently on a large table, you'll want specialized indexes or a dedicated search mechanism.
UNKNOWN in combined conditions and the NOT IN trap
In the previous lesson, you saw that NULL introduces a third logical result:
UNKNOWN
This matters for IN too.
As long as you write the list yourself and it contains only ordinary values, everything is straightforward:
WHERE category IN ('Books', 'Toys')
This is similar to:
WHERE category = 'Books'
OR category = 'Toys'
But NOT IN behaves like a chain of conditions joined with AND.
For example:
WHERE category NOT IN ('Books', 'Toys')
is similar to:
WHERE category <> 'Books'
AND category <> 'Toys'
This is where the trap appears.
Look at this:
SELECT 'found'
WHERE 1 NOT IN (2, 3);
This query returns a row because:
1 <> 2 AND 1 <> 3
is:
TRUE AND TRUE
The result is TRUE.
Now look at this:
SELECT 'found'
WHERE 1 NOT IN (2, NULL);
This query doesn't return a row.
Why?
NOT IN (2, NULL) expands approximately to:
1 <> 2 AND 1 <> NULL
The first part:
1 <> 2
returns TRUE.
The second part:
1 <> NULL
returns UNKNOWN.
So the result is:
TRUE AND UNKNOWN
which gives UNKNOWN.
And WHERE only lets TRUE through.
So the row doesn't appear in the result.
The most dangerous situation isn't when you type the list yourself. In that case, you can see the NULL directly.
The real danger appears with :
WHERE product_id NOT IN (
SELECT product_id
FROM archived_products
)
If the subquery returns even one NULL, the result may unexpectedly be empty.
For now, remember this warning sign:
NOT IN+ a possibleNULL= risk of an empty result.
In later modules, once we introduce subqueries, you'll see safer alternatives using NOT EXISTS.
What happens when the value is NULL
BETWEEN, IN, and LIKE also follow NULL logic.
If a value is missing, an ordinary check doesn't become TRUE.
For example:
price BETWEEN 10 AND 30
If price is NULL, the result is UNKNOWN.
category IN ('Books', 'Toys')
If category is NULL, the result is UNKNOWN.
name LIKE 'Coffee%'
If name is NULL, the result is UNKNOWN.
And WHERE only lets TRUE through.
So rows with NULL don't pass these filters on their own.
If you need to include them, add an explicit condition:
WHERE price BETWEEN 10 AND 30
OR price IS NULL
or:
WHERE category IN ('Books', 'Toys')
OR category IS NULL
BETWEEN, IN, and LIKE don't handle missing values separately. With NULL, you still need IS NULL and IS NOT NULL.
Common mistakes
Mistake 1. Forgetting that BETWEEN includes the boundaries
WHERE price BETWEEN 10 AND 30
This includes both 10 and 30, meaning $10.00 and $30.00.
If the boundaries shouldn't be included, write:
WHERE price > 10
AND price < 30
Mistake 2. Using BETWEEN with dates and accidentally cutting off the last day
Be careful with:
WHERE created_at BETWEEN '2024-01-01' AND '2024-01-31'
If created_at stores a time as well, some records from January 31 may be left out.
It's often safer to write:
WHERE created_at >= '2024-01-01'
AND created_at < '2024-02-01'
Mistake 3. Writing lots of OR conditions instead of IN
You can write:
WHERE category = 'Books'
OR category = 'Toys'
OR category = 'Accessories'
But this is easier to read:
WHERE category IN ('Books', 'Toys', 'Accessories')
Mistake 4. Thinking IN searches for similar text
WHERE category IN ('Books')
won't find:
Electronic Books
That's because IN checks for an exact match.
To search for similar text, use LIKE:
WHERE category LIKE '%books%'
Or use ILIKE if you need a case-insensitive search in PostgreSQL:
WHERE category ILIKE '%books%'
Mistake 5. Confusing % and _
LIKE 'A%'
A, followed by any number of characters.
LIKE 'A_'
A, followed by exactly one character.
Mistake 6. Forgetting to escape % and _
If you need to find a % sign, don't write:
WHERE name LIKE '%50%%'
Escape it explicitly instead:
WHERE name LIKE '%50!%%' ESCAPE '!'
Mistake 7. Putting % at the beginning of a pattern and expecting it to be fast
WHERE name LIKE '%coffee%'
This kind of search may be fine on a small table, but it often becomes expensive on a large one.
Interview question
Interview question:
How does BETWEEN 10 AND 30 differ from price > 10 AND price < 30?
Strong answer:
BETWEEN includes both boundaries. In other words, price BETWEEN 10 AND 30 is equivalent to price >= 10 AND price <= 30. The condition price > 10 AND price < 30 excludes the boundaries.
Interview question:
When should you use IN, and when should you use several conditions joined with OR?
Strong answer:
When the same column is compared with several exact values, IN is usually easier to read. For example, category IN ('Books', 'Toys') is clearer than category = 'Books' OR category = 'Toys'. Conceptually, it checks whether the value is in a list.
Interview question:
What's the difference between % and _ in LIKE?
Strong answer:
% means any number of any characters, including zero characters. _ means exactly one character of any kind. So LIKE 'A%' finds A, AB, and ABC, while LIKE 'A_' finds only two-character strings that start with A.
Interview question:
From a performance perspective, how does LIKE 'abc%' differ from LIKE '%abc'?
Strong answer:
With LIKE 'abc%', the prefix of the string is known. The can potentially use an index much like a range lookup in a sorted dictionary. With LIKE '%abc', the beginning of the string is unknown: the match can start anywhere, so a regular index on the column often doesn't help, and the database has to inspect many rows or the entire table. If you frequently need to search in the middle of strings, PostgreSQL users typically consider trigram indexes with pg_trgm or .
Interview question:
Why can NOT IN unexpectedly return an empty result?
Strong answer:
NOT IN is dangerous when the list or result contains NULL. For example, 1 NOT IN (2, NULL) is conceptually equivalent to 1 <> 2 AND 1 <> NULL. The first part returns TRUE, the second returns UNKNOWN, so the overall result is UNKNOWN. Since WHERE only lets TRUE through, the row isn't returned. If the list comes from a subquery and NULL is possible, you need to be especially careful.
price BETWEEN 10 AND 30?category = 'Books' OR category = 'Toys'?full_name LIKE 'A%' find?code LIKE 'A_' mean?LIKE '%coffee%' be slow on a large table?QUERY: A range, a list, and a stencil are three quick ways to ask the archive a question. What matters is knowing when the pattern helps and when it makes the archive reread everything from beginning to end.
- Patients with Maple Ave in Their AddressEASY
- Passengers with Hotmail AddressesEASY
- Kotomarket showcase: three categories with INEASY
SELECT name, category, price FROM products WHERE price BETWEEN 10 AND 30 ORDER BY price;