# IN, BETWEEN & LIKE

> Write shorter filters with IN for lists, BETWEEN for ranges and LIKE for text patterns, and avoid the NOT IN trap.

Subject: SQL | Lesson 8 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/sql/in-between-and-like
Previous: [WHERE Filters](https://codewithanimation.in/sql/where-filters.md) | Next: [ORDER BY, LIMIT & OFFSET](https://codewithanimation.in/sql/order-by-limit-offset.md)

## Three shortcuts for common tests

`IN` checks a value against a list. `team IN ('Red', 'Green')` is the same as `team = 'Red' OR team = 'Green'`.

`BETWEEN` checks a range, and it includes both ends. `points BETWEEN 17 AND 24` means `points >= 17 AND points <= 24`.

`LIKE` matches text against a pattern. `%` stands for any run of characters, even none. `_` stands for exactly one character. The pattern must match the whole text.

## Shorter queries, fewer mistakes

A long chain of `OR` is easy to get wrong. `IN` says the same thing in one line. A range written with `BETWEEN` cannot forget one of its two ends.

`LIKE` is how you search text without knowing the exact value, such as every name that starts with A.

## Use each one

Three queries on `players`, with the rows they keep:

```sql
WHERE team IN ('Red', 'Green')       -- Asha, Chen, Dara, Fay, Gus
WHERE points BETWEEN 17 AND 24        -- Ben, Dara, Fay, Hana
WHERE name LIKE '_a%'                 -- Dara, Fay, Hana
```

`_a%` means one character, then `a`, then anything. That is why Dara, Fay and Hana match, and Asha does not.

Put `NOT` in front to flip the test: `NOT IN`, `NOT BETWEEN`, `NOT LIKE`. In PostgreSQL, `ILIKE` is a case-insensitive `LIKE`. It is an extension, not standard SQL.

## Common mistake: Three traps

`NOT IN` with a `NULL` in the list never keeps a row. `points NOT IN (17, NULL)` is false for 17 and unknown for everyone else, so the result is empty. When a list can hold NULL, filter the NULLs out of it first.

`BETWEEN` with the ends swapped matches nothing: no number is both at least 24 and at most 17. `BETWEEN SYMMETRIC` swaps them for you.

`LIKE` is case sensitive and needs the whole text to match. `name LIKE 'a%'` finds nobody here, because every name starts with a capital letter.

## Tip: Search anywhere in the text with %

To find text anywhere inside a value, put `%` on both sides: `name LIKE '%an%'`. Without the `%` signs, the pattern has to match the whole value.

## Try it: Try the shortcuts and the traps

Switch between IN, BETWEEN and LIKE, pick a condition, and read TRUE, FALSE or UNKNOWN for every player.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/in-between-and-like

## Check yourself

### Question 1

Which players does `points BETWEEN 17 AND 24` keep?

- A. Players with 17 to 24 points, including exactly 17 and exactly 24
- B. Players with more than 17 and fewer than 24 points
- C. Players with 17 or 24 points only

Correct answer: A

BETWEEN is inclusive: it means points >= 17 AND points <= 24.

- Why not B: BETWEEN includes both ends.
- Why not C: BETWEEN covers the whole range, not just the two ends.

### Question 2

Which names does `name LIKE '_a%'` match?

- A. Dara, Fay and Hana
- B. Asha, Dara and Hana
- C. Only names that are exactly two letters long

Correct answer: A

_ is exactly one character, then a, then % for anything. The second letter must be a.

- Why not B: Asha has s as its second letter. Names ending in a would be '%a'.
- Why not C: The % after the a allows any number of extra characters.

### Question 3

What does `WHERE points NOT IN (17, NULL)` return?

- A. No rows
- B. Every player whose points are not 17
- C. An error, because NULL cannot be in a list

Correct answer: A

NOT IN means points <> 17 AND points <> NULL. The second part is never true, so no row is kept.

- Why not B: The NULL in the list turns every other comparison into unknown.
- Why not C: It is allowed. It just makes NOT IN unable to be true.

## Remember this

- IN is a short way to write a chain of OR tests on one column.
- BETWEEN includes both ends, so a BETWEEN 17 AND 24 means a >= 17 AND a <= 24.
- LIKE uses % for any run of characters and _ for one character, and the pattern must match the whole text.
- NOT IN with a NULL in the list never keeps a row, so remove NULLs from the list first.
