QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › SELECT and filtering

How do IN, BETWEEN and LIKE work?

IN checks a match against a list, BETWEEN sets a range with both bounds included, and LIKE searches by pattern (% is any characters, _ is one). Traps: BETWEEN on a timestamp cuts off the last day, so a half-open interval is used; NOT IN with a NULL in the list returns zero rows; LIKE is case-sensitive in PostgreSQL, and a pattern with a leading % usually cannot use a regular index.

SQL · SELECT and WHERE

The September report does not show transactions from 30 September. The data is there and the range is "inclusive". Where did they go?

The September report is written with BETWEEN '2026-09-01' AND '2026-09-30'. A transaction from 30 September at 14:20 is in the database and not in the report, and nobody notices because the total is just slightly lower. This lesson covers three operators that look simple: IN, BETWEEN and LIKE, and each has its own trap.

How it works and why. All three are shorthands for conditions you could write by hand, and that makes their behaviour clear.

IN checks a match against a list: status IN ('pending', 'failed') is the same as status = 'pending' OR status = 'failed'. Two conclusions follow: a comparison with a NULL in the list also gives unknown, and NOT IN turns this into a trap (below).

BETWEEN a AND b is the same as >= a AND <= b: both bounds are included. If you swap them (BETWEEN 500 AND 100) the result is empty because the condition is impossible. PostgreSQL has BETWEEN SYMMETRIC for that case.

LIKE compares a string with a pattern: % is any number of characters (even zero), _ is exactly one character. Everything else is taken literally.

PatternMatchesDoes not match
a%a, ab, abcba
%@gmail.comanna@gmail.comanna@gmail.com.ua
a_cabc, a1cac, abbc
100\%100% (escaped %)1000

Example: from BETWEEN to a half-open range. The created_at column is a time type. The literal '2026-09-30' compared with it means 2026-09-30 00:00:00, so everything that happened that day after midnight lies beyond the bound.

-- created_at is a timestamp; the row 2026-09-30 14:20 disappears
SELECT id, created_at
FROM transactions
WHERE created_at BETWEEN '2026-09-01' AND '2026-09-30';

What BETWEEN cuts off with dates

BETWEEN … AND 2026-09-30
idcreated_at
109-01 00:00
209-15 10:05
309-30 14:20 lost
>= 09-01 AND < 10-01
idcreated_at
109-01 00:00
209-15 10:05
309-30 14:20
Figure 1. A half-open range does not depend on whether the column has a time and does not need to know how many days the month has.

A half-open range fixes it: the start is included, the next day is excluded.

-- a half-open range: includes the start, excludes the next day
SELECT id, created_at
FROM transactions
WHERE created_at >= '2026-09-01'
  AND created_at <  '2026-10-01';

SELECT email FROM users WHERE email ILIKE '%@gmail.com';
-- ILIKE: a PostgreSQL extension; else lower(email) LIKE ...

The same applies to numbers with a fractional part: BETWEEN 0 AND 10 will not catch 10.5 but will catch 10, so choose the bounds deliberately.

Limits and alternatives. For timestamptz a literal without a time zone is taken in the current session's zone, so the month bound shifts with the client setting: a report run from another zone gives different numbers. That is why you set the bound explicitly ('2026-09-01 00:00+00') or agree on the session zone. See the lesson on dates and time zones. In LIKE case depends on the database: in PostgreSQL LIKE is case-sensitive and ILIKE is not; in MySQL the column's collation decides. A pattern with a leading % usually cannot use a regular index: the search scans the whole table (PostgreSQL has the pg_trgm extension for such cases). To find a literal % or _ you escape it: in PostgreSQL the default escape character is the backslash, and you can set another with ESCAPE. And about NOT IN:

SELECT * FROM transactions WHERE status IN ('pending', 'failed');
-- the same as
--   status = 'pending' OR status = 'failed'

SELECT * FROM transactions WHERE status NOT IN ('completed', NULL);
-- always 0 rows: a NULL in the list makes the condition unknown

The zero rows here come from the NULL in the list: status <> NULL is unknown for every row. The same trap appears when NOT IN takes values from a subquery that contains a NULL, and the lesson on NOT IN with a subquery covers it.

Common mistakes.

  • BETWEEN on a timestamp ending on "the 30th": the last day is cut off.
  • Swapped bounds BETWEEN 500 AND 100: an empty result.
  • _ in a pattern taken as an ordinary character: LIKE 'user_1' also finds userX1.
  • LIKE ignoring case in a database where it is case-sensitive: "no data".
  • NOT IN with a list or subquery that contains NULL.

Check yourself

Does amount BETWEEN 100 AND 500 include the values 100 and 500?

Answer

Yes, both bounds are included: it is amount >= 100 AND amount <= 500. If a bound must not be in the result, use separate >= and <.

A transaction was created at 2026-09-30 23:59 in a timestamp column. Will it get into WHERE created_at BETWEEN '2026-09-01' AND '2026-09-30'?

Answer

No. The literal '2026-09-30' means 2026-09-30 00:00:00, so anything later lies above the upper bound. The right way: created_at >= '2026-09-01' AND created_at < '2026-10-01'.

How do you find emails that contain a literal underscore?

Answer

Escape it: email LIKE '%\_%'. Without escaping the underscore means any single character and the condition matches almost everything.

In an interview. Junior: IN checks membership in a list, BETWEEN is a range including both bounds, and LIKE searches by a pattern where percent is any number of characters and underscore is one character. I do not use BETWEEN with dates: instead I write "greater than or equal to the start and less than the next day". Middle/Senior: All three are shorthands for ordinary conditions, so their traps are predictable: BETWEEN includes the bounds and cuts the last day for a timestamp, NOT IN with a NULL gives an empty result, LIKE is case-sensitive in PostgreSQL and cannot use an index with a leading percent. I set period bounds as a half-open range and account for the time zone.

More articles in this chapter