Choose SQL predicates for ranges, membership and patterns

Select comparison, membership, range and pattern predicates while keeping NULL and endpoint boundaries visible. Predicates and safe expressions · Databases.

A filter can miss a row because its endpoint, membership list or pattern means something different from the request. The predicate is part of the request's meaning, so read its boundary before reading its row count.

Compare values and name the range

Comparison operators ask how one value relates to another. Equality asks for the same known value; <, <=, > and >= describe an ordering when the data type has one. BETWEEN lower AND upper includes both endpoints. NOT BETWEEN is the negation of that inclusive test, so a value at either endpoint is still inside the range.

For timestamps, a calendar request is usually clearer as a half-open interval: event_at >= TIMESTAMP '2026-04-01 00:00:00' AND event_at < TIMESTAMP '2026-05-01 00:00:00'. The next period's start is an exclusive upper boundary. This avoids inventing a final second or fractional precision for the previous day. The same reasoning applies to a numeric range when the request explicitly excludes its upper endpoint. BETWEEN remains useful when both endpoints are genuinely included.

The WHERE contract from the prerequisite still applies: only TRUE rows survive. A NULL timestamp or amount does not become a match merely because a range looks broad. Its comparison is UNKNOWN, and the row is filtered out. If a report needs missing values as a separate category, add an explicit IS NULL branch rather than stretching the range predicate to cover it.

Predicate choices on known and missing values

Read the steps as text

  1. A predicate sees a known amount, a missing amount, and a list with one missing member. The query also names an inclusive range and an escaped pattern.
  2. The known amount 25 is included because BETWEEN includes both endpoints of the stated range.
  3. West does not match north, but the NULL list member makes NOT IN UNKNOWN; WHERE drops the row.
  4. The code pattern contains an escaped underscore. The escape marker makes the underscore literal while the percent wildcard can still match a suffix.
  5. The predicate family is now separated: comparisons handle known bounds, NULL-aware predicates handle missingness, and LIKE plus ESCAPE handles text patterns.

The first frame presents predicate inputs: numeric bounds, a boundary amount, a timestamp boundary and a missing amount. The range comparison includes both numeric endpoints, while the timestamp window excludes the next period's start and a missing amount stays separate. The membership example then tests a city against a NOT IN list containing NULL. The pattern example shows a literal underscore in a code pattern.

Membership is a Boolean question with a NULL boundary

IN (v1, v2, v3) is a compact membership test. If a known left value equals one listed value, the predicate is TRUE. NOT IN expresses that no listed value matches, but it inherits the uncertainty of SQL's three-valued logic. If there is no match and either the left value or a list member is NULL, the result can be UNKNOWN rather than TRUE. A WHERE clause drops that row.

For a known value, a matching member makes NOT IN FALSE even if another list member is NULL. The NULL trap concerns the no-match case: the database cannot prove that the unknown member differs from the left value. Keep exclusion lists free of NULL when that is the intended contract, and use IS NULL or an explicitly grouped condition when the report has a separate missing-value policy. Do not replace value = NULL with a guess; use IS NULL to ask whether the value is missing and IS NOT NULL to ask whether it is present. Empty text and zero are known values and need their own comparisons.

Make patterns say what is literal

In the PostgreSQL 18 profile used here, LIKE tests the complete string against a pattern. % matches zero or more characters and _ matches exactly one character. A pattern such as 'AB%' therefore describes strings beginning with AB; it does not mean that AB may appear anywhere. A pattern such as '%AB%' asks for a substring anywhere in the value. NOT LIKE negates the match, but a NULL value still produces UNKNOWN and is not retained by WHERE without an explicit missing-value branch.

A wildcard is not automatically literal. Use an explicit escape character when the request contains a real percent or underscore. For example, code LIKE 'acct#_%' ESCAPE '#' treats the underscore after acct as data and lets the final percent match a suffix. The ESCAPE choice belongs to the pattern expression; do not rely on a backslash spelling when a profile, string-literal setting or dialect changes how that character is parsed. Case behavior depends on the database's comparison rules. PostgreSQL's ILIKE is a profile-specific case-insensitive extension, not a portable synonym for LIKE.

When conditions combine, put the intended branches in parentheses. (region = 'east' OR region = 'west') AND active means an allowed region plus active status. Without grouping, operator precedence can make a row satisfy active only on one branch. The predicate should mirror the business sentence before the query is tested against data.

Faded predicate transfer

Read the steps as text

  1. A new event query combines a half-open date boundary, a region choice, and a literal underscore in a code.
  2. The next period begins at Tuesday, so the end boundary is excluded. This prevents one timestamp from belonging to both adjacent windows.
  3. The new code uses the same escape marker. The underscore is literal, while the percent can cover the remaining suffix.
  4. The fresh case keeps the same discipline: make time boundaries explicit, group OR choices, and declare literal pattern characters with ESCAPE.

The faded table uses new event codes and a new date boundary. The half-open reporting interval excludes its end. The pattern escapes a literal underscore while a percent remains a wildcard. A final branch groups two allowed regions with an active flag. The rows change, but the decision boundaries stay visible.

Recap

  • Comparison operators describe ordered values; BETWEEN includes both endpoints, while a half-open timestamp interval can name the next period's start.
  • IN, NOT IN, IS NULL and IS NOT NULL answer different questions, and an unmatched NULL operand can make NOT IN UNKNOWN.
  • LIKE uses % and _ as wildcards; explicit ESCAPE and parentheses keep pattern and Boolean boundaries visible.
A predicate is a contract, not a row-count trick

A row count cannot tell you whether a missing row was outside the range, failed membership, matched UNKNOWN, or was hidden by an unescaped wildcard. Name the value domain, endpoint policy, missing-value policy and pattern grammar first. PostgreSQL 18 documents LIKE as a whole-string match with an optional ESCAPE character; other engines can differ in collation, case extensions and literal-string rules, so carry the engine profile with the example.

Next, Projection, order and safe expressions builds a result after the predicate has decided which rows survive.