Scrivio Interview library
CodingFoundations7 min read · updated 12 November 2026

Rewrite each predicate so an index on the column can be used: (1) WHERE DATE(created_at) = '2026-03-01'; (2) WHERE LOWER(email) = 'a@b.com' with an index on email; (3) WHERE amount + 10 > 100; (4) WHERE name LIKE '%son'.

A predicate can use an index when it compares the bare indexed column with a value, so the optimizer can seek a range of the index. So we rewrite DATE(created_at) = '2026-03-01' as the range created_at >= '2026-03-01' AND created_at < '2026-03-02', handle LOWER(email) with an expression index on LOWER(email), citext or a normalised stored value, move the arithmetic to the constant side as amount > 90, and replace the leading-wildcard LIKE '%son' with a pg_trgm GIN index, a reversed-column index or full-text search.

Why this question is asked

Wrapping an indexed column in a function is one of the most common reasons an index is silently ignored, and it is easy to write by accident in both SQL and ORM code. Interviewers use exercises like this one to see whether we know why the index stops working, not just the rewritten text. In this answer, we will recap what sargable means and why a B-tree needs the bare column, and then fix each of the four predicates in turn, showing the broken plan, the rewrite and why it works.

1A Quick Recap of Sargable Predicates

Sargable comes from search argument able. A predicate is sargable when the database can use it to search an index directly. A B-tree index, the default index type, stores the column's values in sorted order with a pointer to each row. To use it, the optimizer needs a condition it can translate into a position or a range in that sorted order: equal to 42, between two dates, starts with 'abc'.

If we write a function or arithmetic around the column, the index still holds the original values, but the condition talks about computed values the index does not store. The database would have to compute the function for every row to check the condition, which means a full scan. The rule of thumb is simple: keep the indexed column alone on one side of the comparison, and move everything else to the other side.

2Fix 1: Replace DATE(created_at) With a Range

The index on created_at stores timestamps such as 2026-03-01 14:22:05. DATE(created_at) produces a different value, the date only, so the index cannot be searched for it. In PostgreSQL the same thing is written created_at::date.

SQL
-- broken: function on the column
SELECT * FROM orders WHERE DATE(created_at) = '2026-03-01';
-- Seq Scan on orders Filter: (date(created_at) = '2026-03-01'::date)
-- fixed: a half-open range on the bare column
SELECT * FROM orders
WHERE created_at >= '2026-03-01' AND created_at < '2026-03-02';
-- Index Scan using orders_created_at_idx
-- Index Cond: ((created_at >= '2026-03-01') AND (created_at < '2026-03-02'))

Notice that the range is half-open: greater than or equal to the start, strictly less than the next day. This includes every timestamp on March 1, including 23:59:59.999999, without having to guess the last possible value, which BETWEEN '2026-03-01' AND '2026-03-01 23:59:59' would get wrong for fractional seconds. In Java we compute the bounds with LocalDate: date.atStartOfDay() and date.plusDays(1).atStartOfDay(). For a timestamptz column, remember that the day boundaries depend on the time zone we mean.

3Fix 2: Case-Insensitive Email Lookups

LOWER(email) = 'a@b.com' cannot use a plain index on email for the same reason: the index stores 'A@B.com', not its lowercase form. There are three good options, and the right one depends on what the data means.

SQL
-- option A: an expression index that stores the computed value
CREATE INDEX users_email_lower_idx ON users (LOWER(email));
SELECT * FROM users WHERE LOWER(email) = 'a@b.com';
-- Index Scan using users_email_lower_idx (the expression must match exactly)
-- option B: store the email normalised, keep the plain index
INSERT INTO users (email) VALUES (LOWER(:email));
SELECT * FROM users WHERE email = 'a@b.com';
-- option C: PostgreSQL citext, a case-insensitive text type
CREATE EXTENSION IF NOT EXISTS citext;
ALTER TABLE users ALTER COLUMN email TYPE citext;
SELECT * FROM users WHERE email = 'A@b.com'; -- matches 'a@B.com', uses the index on email

An expression index is the smallest change: the query stays the same, and the index stores LOWER(email) for every row. The query's expression must match the index expression exactly. Storing normalised values is the simplest at query time but changes what we keep. citext makes comparisons case-insensitive by type, so the plain index works. In MySQL, the default collations are usually already case-insensitive, so email = 'A@b.com' matches without LOWER, and adding LOWER there only breaks the index.

4Fix 3: Move Arithmetic to the Constant Side

WHERE amount + 10 > 100 asks the database to compute amount + 10 for every row. Most optimizers do not solve the inequality for us, so the index on amount is not used. We do the algebra ourselves.

SQL
-- broken
SELECT * FROM payments WHERE amount + 10 > 100;
-- Seq Scan on payments Filter: ((amount + 10) > 100)
-- fixed: same rows, bare column
SELECT * FROM payments WHERE amount > 90;
-- Index Scan using payments_amount_idx Index Cond: (amount > 90)

The two conditions return exactly the same rows for numeric columns, but only the second gives the optimizer a range in the index, everything above 90. The same applies to amount * 1.2 > 100, which becomes amount > 100 / 1.2, computed once.

5Fix 4: Leading Wildcards Need a Different Index

LIKE 'son%' is sargable: all names starting with son sit together in a sorted index. LIKE '%son' is not, because names ending in son are scattered through the sorted order, from Anderson to Wilson. No rewrite of the predicate helps a B-tree here, so we need a different kind of index.

SQL
-- option A: trigram index (PostgreSQL pg_trgm)
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE INDEX people_name_trgm ON people USING gin (name gin_trgm_ops);
SELECT * FROM people WHERE name LIKE '%son';
-- Bitmap Index Scan on people_name_trgm
-- option B: index the reversed value, then search a prefix
CREATE INDEX people_name_rev ON people (reverse(name) text_pattern_ops);
SELECT * FROM people WHERE reverse(name) LIKE 'nos%'; -- 'son' reversed
The four rewrites
OriginalRewriteWhy it works
DATE(created_at) = '2026-03-01'created_at >= '2026-03-01' AND < '2026-03-02'bare column, a range in the index
LOWER(email) = 'a@b.com'index ON users (LOWER(email)), or citextthe index stores the computed value
amount + 10 > 100amount > 90arithmetic moved to the constant
name LIKE '%son'pg_trgm GIN index, or reverse(name) LIKE 'nos%'a B-tree cannot seek a suffix

A trigram index splits each value into three-character pieces and can find values containing given pieces anywhere, so it supports '%son', '%son%' and even ILIKE. It is larger and slower to update than a B-tree. The reversed-column index is small and fast but only handles suffix searches. If the real need is searching words in text, full-text search with a tsvector column and a GIN index is the right tool.

6Follow-up questions

Why does an implicit type cast on a column break index use?

Because a cast is just a function applied to the column. If we compare a varchar column with a number in MySQL, the server converts each column value to a number to compare, so it must evaluate every row and the index on the string values cannot be searched. In PostgreSQL, a mismatched type may force a cast on the column side, such as an explicit column::text, with the same effect. The fix is to compare with a value of the column's own type, which in Java means binding the parameter with the matching setter or entity field type.

Can an expression index be used if my query writes the expression slightly differently?

Usually not. The optimizer matches the expression in the query against the expression in the index definition, so LOWER(email) and lower(email) match, but LOWER(TRIM(email)) does not. It is good practice to keep such expressions in one place, for example a repository method, so they stay identical.

Is OR on two different columns sargable?

Each side can be, but one B-tree cannot serve both. PostgreSQL can combine two indexes with a BitmapOr, and MySQL has index merge, but often rewriting as a UNION of two indexed queries, or adding the right indexes, is more reliable.

7Conclusion

In this answer, we saw that a sargable predicate keeps the indexed column bare on one side so the optimizer can seek a range of the index. We turned DATE(created_at) into a half-open range, handled LOWER(email) with an expression index, citext or normalised values, moved arithmetic to the constant side to get amount > 90, and served the leading wildcard '%son' with a pg_trgm GIN index, a reversed-column index or full-text search, because no B-tree can seek a suffix.

What to say in the interviewAbout 90 seconds aloud

Open with

The general rule is that an index can be used when the indexed column stands alone on one side of the comparison, because the B-tree stores the column's raw values in sorted order and the optimizer needs to turn the condition into a range of that order.

Then, if they want more

A function or arithmetic on the column means it would have to compute the value for every row, which is a scan. So for the first one, DATE of created_at, I would write a half-open range: created_at greater than or equal to March 1 and less than March 2. That includes every timestamp in the day, fractional seconds too.

For LOWER of email with an index on email, I have three choices: an expression index on LOWER of email so the query stays as it is, storing emails already normalised, or in PostgreSQL the citext type, which compares case-insensitively and uses a normal index.

For amount plus 10 greater than 100, I just move the arithmetic to the constant side and write amount greater than 90, which returns the same rows. The last one, LIKE with a leading percent, cannot be fixed with a rewrite, because names ending in son are scattered through a sorted index.

There I would use a pg_trgm GIN index, which also covers contains searches, or an index on the reversed name with a prefix search, or full-text search if we are really searching words.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Rewrite each predicate so an index on the column can be used: (1) WHERE DATE(created_at)...": https://scrivio-interview-library.netlify.app/databases/query-optimization/270e0ff2a0/