Filtering & predicates
Concept  ·  SQL & Querying

Comparing a string to a date/number forces an implicit cast — silently wrong ordering or filtering.

The column's type decides the comparison rules, not the values' looks. Text ordering is character-by-character, so '1…' anything precedes '2'; text MAX crowns '9'. Meanwhile equality with a numeric literal can coerce and 'work', which is how text/int keys join for months before a non-numeric value appears and the cast explodes. Fix the type at the source; until then cast explicitly and visibly at every comparison.

See it break
slips
slip
9
10
2
15
100
The trap
SELECT
  slip
FROM
  slips
ORDER BY
  slip ;
The fix
SELECT
  slip
FROM
  slips
ORDER BY
  CAST(slip AS INT);
The trap
10
100
15
2
9
The fix
2
9
10
15
100

The slip numbers are text, so ORDER BY sorted them character-by-character — '10' and '100' came before '2', and MAX would crown '9' instead of the real highest slip.

The rule

Fix the schema, or write CAST(slip AS INT) out where the debt lives.

DuckDB and Postgres error on text_col > 5; looser engines coerce silently — the errorless version is the dangerous one.

Shows up elsewhere
BETWEEN on timestamps — the midnight trapSet ops align by position
Try one →Prove it in practice →