Learn SQL
-- filtering.sargability_indexable_where

Sargable WHERE — Index-Seekable Filters in SQL

Bare column vs value seeks; functions force scans

--concept

Sargable predicates let the engine seek: bare column compared to a value (col = ?, col > ?, col BETWEEN). Non-sargable forms — functions on columns (YEAR(hired)), leading-% LIKE, arithmetic wrapping, implicit type conversions — force full scans. Write filters index-first, match types, keep columns bare; fix legacy shapes with expression indexes.

--how it works
1

Seekable shape

Bare column vs value: col = ?, col > ?, BETWEEN — index seeks O(log n).

2

Scan-forcing shapes

Functions, leading-%, arithmetic wrapping, type mismatches — full scans O(n).

3

Rewrite, don’t decorate

YEAR(hired)=2024 → hired range. salary*12>x → salary>x/12.

4

Fix legacy with indexes

Expression/computed indexes make functional predicates seekable when rewrite is impossible.

--visualization
hired >= 2024-01-01 AND hired < 2025-01-01 → Index SeekSame logic, O(log n) seek
Stage 1 / 1
Write filters index-first: bare columns, matching types.
--example
query.sql
1-- slow: WHERE YEAR(hired) = 2024;
2-- fast: WHERE hired >= DATE '2024-01-01' AND hired < DATE '2025-01-01';
Result
formplan
slowfunction on column → scan
fastbare column range → seek

Both ask for 2024 hires. The range seeks the hired index to January then scans forward; the function evaluates YEAR per row across the whole table.

--variants

YEAR() → range (same logic, seek)

Identical answers, different plans.

query.sql
1-- slow: WHERE YEAR(hired) = 2024 (scan)
2-- fast: WHERE hired >= DATE '2024-01-01' AND hired < DATE '2025-01-01' (seek)
Result
formplan
slowper-row function → scan
fastbare range → seek
1Function evaluates per row across the table.
2Range seeks January then scans forward.
3EXPLAIN confirms Index vs Seq Scan.

LIKE prefix vs leading-%

Left-anchored serves B-trees; leading-% needs trigrams.

query.sql
1-- fast: WHERE name LIKE 'A%' (prefix seek)
2-- slow: WHERE name LIKE '%son' (scan; use trigram/full-text)
Result
patternplan
A%prefix seek
%sonscan without trigram
1B-tree serves left-anchored prefixes.
2Leading-% cannot navigate sorted pages.
3Substring search wants pg_trgm/GIN or full-text.
--key facts
Sargablebare column vs value — seekable
Killersfunctions, leading %, conversions
Fixrewrite predicate or add expression index
--use cases
Slow-filter triage: function-wrapped columns
Review gate: sargability check per WHERE predicate
--under the hood
Planners estimate selectivity from stats — functions hide it; bare ranges expose it.
Expression index stores computed values — seeks return without per-row evaluation.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.