SARGable Queries (Writing Index-Friendly WHERE Clauses)
Write predicates the optimizer can seek with indexes (Search ARGument Able).
In plain words
Leave the indexed column alone in the WHERE clause, and put any calculation on the other side, so the database can jump straight to the rows.
Think of it like…
A dictionary sorted by word: finding words starting with "pre" is a quick jump. Finding words whose *third* letter is "e" means reading every page, even though the dictionary is sorted.
Where you’ve seen it
WHERE DATE(created_at) = '2026-11-19' scans every order. WHERE created_at >= '2026-11-19' AND created_at < '2026-11-20' means the same thing and uses the index on created_at.
Words you’ll hear
- SARGable
- A predicate shape the optimizer can turn into an index seek or range scan.
- Index seek
- Jumping straight to matching index entries instead of scanning.
- Expression index
- An index on a computed value, such as LOWER(email), so that expression becomes searchable.
- Implicit conversion
- The database silently casting a column to compare it with a different type, which can disable the index.
Visual diagram
Theory & example queries
Shapes that break index use
A B-tree index stores the column's values in sorted order. It can answer questions about those values directly (=, <, >, BETWEEN, IN, prefix LIKE 'abc%'), but not about a transformed version of them. LOWER(email) = ?, price * 1.18 > 100, created_at + interval '1 day' > now() and COALESCE(status, 'new') = 'new' all apply something to the column first, so the index can't be searched.
Other common culprits: a leading wildcard (LIKE '%gmail.com'), implicit type conversions (in MySQL, comparing a VARCHAR phone column with a number casts every row), and wrapping columns in functions inside joins.
WHERE price * 1.18 > 1180 can't use an index on price; WHERE price > 1000 (1180 ÷ 1.18) can.
How to fix them
Rewrite the condition so the bare column is compared with a value: date functions become ranges, arithmetic moves to the other side, and col + 1 = 5 becomes col = 4. Ranges written as >= start AND < next_start also avoid off-by-one errors with timestamps.
When the transformation is part of the logic, such as case-insensitive email lookup, index the expression itself: CREATE INDEX ON users (LOWER(email)), then query with exactly the same expression. For suffix or substring search, use a trigram index (pg_trgm) or full-text search rather than LIKE '%…%'.
CREATE INDEX idx_users_email_lower ON users (LOWER(email));PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
SELECT *FROM usersWHERE LOWER(email) = LOWER($1);PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Representative lookup result1 sample row
| id | name | |
|---|---|---|
| 101 | ada@example.com | Ada Patel |
Note: Assumes $1 is case-insensitively equal to "ada@example.com" and the users table has this one match.
then
Key takeaway: Rewrite to a bare column, or index the exact expression you query.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Spot the scan
Run EXPLAIN on the slow query. A Seq Scan with a Filter on an indexed column is the classic sign of a non-SARGable condition.
Seq Scan … Filter: (date(created_at) = …)
When to use it & trade-offs
What it is
A condition is SARGable (Search ARGument-able) when the database can use an index to seek straight to the matching rows. That usually means the indexed column stands alone on one side of the comparison: created_at >= '2026-01-01' is SARGable, while YEAR(created_at) = 2026 isn't, because the function hides the column from the index.
Why it matters: It's the most common reason an index exists but the query still does a full scan. Rewriting a single WHERE clause can turn a 10-second query into a 10-millisecond one with no new index, which is why interviewers love "why isn't this query using my index?".
When to reach for it
- Date filters on timestamp columns
- Case-insensitive lookups on emails and usernames
- Any query where EXPLAIN shows a Seq Scan despite an index
When to avoid or be careful
- Functions on indexed columns in WHERE and JOIN conditions
- Leading wildcards in LIKE on large tables
- Comparing columns with values of a different type
Fixing a non-SARGable condition
Strengths: Uses the existing index; no new objects.
Watch out: The query has to be written carefully, especially date boundaries.
Strengths: Makes the exact expression searchable.
Watch out: Only helps queries that use the identical expression; another index to maintain.
Strengths: Fast fuzzy and contains-style search.
Watch out: Larger indexes and different query syntax.
Interview signals & numbers to know
When this topic comes up
- “I have an index but the query is still slow”
- “Why isn't my index being used?”
- Seq Scan with a Filter on an indexed column
- WHERE YEAR(date) = …
- LIKE '%…'
Benchmarks & rules of thumb
- >= start AND < next
- the safe pattern for date ranges
- 'abc%'
- can use a B-tree; '%abc' can't
- 1 expression
- per expression index
Common mistakes & follow-ups
Common interview mistakes
- Filtering with DATE(created_at) = '…' and scanning the whole table.
- Writing BETWEEN '2026-11-19' AND '2026-11-19 23:59:59' and missing rows in the last second's fractions.
- Creating an expression index on LOWER(email) but querying with a different expression, like email ILIKE ?.
Likely interviewer follow-ups
- Rewrite WHERE YEAR(order_date) = 2025 so it can use an index.
- Why can LIKE 'abc%' use an index but LIKE '%abc' can't?
- When would you create an expression index instead of rewriting the query?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply SARGable Queries (Writing Index-Friendly WHERE Clauses) with hands-on SQL practice and interview Q&A.