Quick revision
The short answer
Each condition wraps the column in a function, so plain indexes on email, created_at and amount cannot be used; that is the slowness. DATE(created_at) on a timestamptz also depends on the session time zone, so rows near midnight fall on the wrong day and are missed, and ROUND(amount) = 10 matches anything from 9.5 to just under 10.5 with rounding rules that differ between numeric and floating types. The fixes are an expression index or citext for the email, an explicit half-open time range, and an explicit numeric range.
What to remember
In this answer, we saw that all three conditions apply a function to a column, which stops plain indexes from being used. LOWER(email) is fixed with an expression index, citext or normalised data, and in MySQL with a case-insensitive collation it should simply be removed. DATE(created_at) depends on the session time zone and misses rows near midnight, so we replace it with a half-open range in an explicit zone. ROUND(amount) = 10 matches a range the business probably did not mean, with rounding rules that differ by type, so we write the range explicitly.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Each condition wraps the column in a function, so plain indexes on email, created_at and amount cannot be used; that is the slowness. DATE(created_at) on a timestamptz also depends on the session time zone, so rows near midnight fall on the wrong day and are missed, and ROUND(amount) = 10 matches anything from 9.5 to just under 10.5 with rounding rules that differ between numeric and floating types. The fixes are an expression index or citext for the email, an explicit half-open time range, and an explicit numeric range.
Why this question is asked
Functions in WHERE clauses look harmless, and they are one of the most common reasons a query that was fast in development becomes a full table scan in production. Here, three small functions cause two different kinds of problem: performance, because indexes stop working, and correctness, because the functions quietly change what the condition means. Interviewers like this question because a good answer separates the two and fixes each one for the right reason. In this answer, we will recap why a function on a column defeats an index, then take the three conditions one at a time, and finally note how MySQL collations change the email part.
1A Quick Recap: Why a Function on a Column Hides the Index
A B-tree index on email stores the email values in sorted order, so the database can find 'ana@x.com' by walking down the tree in a few steps. But the index stores email, not lower(email). When the condition is lower(email) = 'ana@x.com', the database would need the lowercased values in sorted order, and the index does not have them. Lowercasing can change the order, so the only safe plan is to read every row, compute lower(email), and compare.
A condition that can use an index is often called sargable, short for search-argument-able. As a rule, a predicate is sargable when the indexed column, or an indexed expression, stands alone on one side of the comparison. All three conditions in our query break that rule.
2Problem 1: LOWER(email) and the Index
Calling lower on the parameter is fine; it is computed once. The problem is lower on the column. There are three good fixes. The first is an expression index, which stores the result of the expression and lets the planner use it whenever the query contains exactly the same expression.
The second is to store emails already normalised, lowercased on the way in, and compare plainly. The third, in PostgreSQL, is the citext extension: a case-insensitive text type whose comparisons ignore case, so email = ? matches regardless of case and an ordinary index on the column works. Since PostgreSQL 12, a nondeterministic ICU collation is another way to get case-insensitive comparison.
Lowercasing can also miss rows with non-ASCII letters, where case rules depend on the language: lower of the German sharp s or the Turkish dotted capital I may not give what a user typed. For email addresses this rarely matters in practice, but it is a reason to normalise in one place.
3Problem 2: DATE(created_at) Depends on the Session Time Zone
created_at is a timestamptz, an absolute instant. To turn it into a date, PostgreSQL must decide where in the world we are, and it uses the session's TimeZone setting. The same row can therefore belong to different dates depending on how the connection is configured.
If the application server runs in UTC but the business day is in New York, the query misses every payment made after 20:00 New York time on 1 June and includes some from the previous evening. Nothing fails, the totals are just wrong. The range version names the zone explicitly, so it no longer depends on the connection, and because created_at stands alone it can use an index on created_at. The range is half-open, so a payment at exactly midnight belongs to one day only.
4Problem 3: ROUND(amount) = 10 Rarely Means What the Business Means
ROUND(amount) = 10 matches every amount that rounds to 10: 9.50, 9.99, 10.00, 10.49. Is that what the report wanted? If the question was payments of exactly 10, it includes many that are not. If the question was payments between 9.50 and 10.50, it is better written as a range, which says so openly and can use an index on amount.
| Condition | Why slow | Why wrong | Fix |
|---|---|---|---|
LOWER(email) = LOWER(?) | index on email unused | language-specific case rules | expression index, citext, or normalise |
DATE(created_at) = '2025-06-01' | index on created_at unused | day depends on session time zone | half-open range in an explicit zone |
ROUND(amount) = 10 | index on amount unused | matches 9.50..10.49; float rules differ | explicit range or exact value |
The rounding itself also varies. In PostgreSQL, round on numeric rounds halves away from zero, while round on double precision follows the platform, which is usually round half to even. MySQL does the same split: exact values round half away from zero, approximate values follow the C library. And a floating amount such as 9.499999999 may sit on the other side of a boundary than the decimal the user saw. So the query can both include and miss rows depending on the column type. Money should be stored as numeric or as an integer number of cents, and conditions should compare ranges or exact values, never rounded ones.
5A Note on MySQL Collations
In MySQL, string comparison follows the column's collation, and the default collations, such as utf8mb4_0900_ai_ci, are case-insensitive, the ci at the end, and accent-insensitive, the ai. So email = 'Ana@X.com' already matches 'ana@x.com', and wrapping the column in LOWER adds nothing except that it prevents the index on email from being used. The right fix there is simply to remove LOWER. MySQL 8 also supports functional indexes, written with an extra pair of parentheses, if a function on the column is really needed.
DATE() in MySQL applied to a TIMESTAMP column likewise uses the session time_zone, and a DATETIME column has no zone at all, so the same half-open range approach applies.
6Follow-up questions
Would an index on created_at be used for date(created_at) if I add an expression index?
In PostgreSQL, an expression index on date(created_at) for a timestamptz is not allowed, because the result depends on the session time zone, so the function is not immutable. That restriction is the database telling us the condition itself is ambiguous. An index on (created_at AT TIME ZONE 'UTC')::date is allowed, but the range on created_at is simpler.
Is citext slower than text?
Slightly, because every comparison lowercases both sides, but the difference is small compared with the gain of using an index. The bigger consideration is portability: citext is PostgreSQL-only, so code that also runs elsewhere may prefer normalising emails on write.
How do I spot non-sargable predicates in a large codebase?
Look for slow statements in pg_stat_statements or the slow query log, and check their plans for sequential scans with a Filter line containing a function on a column. Linting SQL for functions applied to columns in WHERE clauses catches many before they ship.
7Conclusion
In this answer, we saw that all three conditions apply a function to a column, which stops plain indexes from being used. LOWER(email) is fixed with an expression index, citext or normalised data, and in MySQL with a case-insensitive collation it should simply be removed. DATE(created_at) depends on the session time zone and misses rows near midnight, so we replace it with a half-open range in an explicit zone. ROUND(amount) = 10 matches a range the business probably did not mean, with rounding rules that differ by type, so we write the range explicitly.
Open with
All three conditions wrap the column in a function, so the planner cannot use plain indexes on email, created_at or amount, and it ends up scanning the whole table and computing the functions for every row.
Then, if they want more
That explains the slowness. Each one also has its own correctness problem. For the email, LOWER on the parameter is fine, but LOWER on the column needs either an expression index on lower(email), or the citext type in PostgreSQL, or emails normalised to lowercase on write.
In MySQL the default collations are already case-insensitive, so LOWER is useless there and only breaks the index; I would just remove it.
DATE of created_at is the one that misses rows: created_at is a timestamptz, and converting it to a date uses the session time zone, so if the server session is UTC and the business day is in New York, evening payments land on the next day.
I replace it with a half-open range, created_at greater or equal to midnight on June 1 and less than midnight on June 2, each converted from the business time zone explicitly. That is both correct and sargable.
ROUND amount equals 10 matches everything from 9.50 up to just under 10.50, and rounding differs between numeric and floating types, half away from zero versus usually half to even. That is rarely what the business means, so I write the intended range or exact value directly.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- MySQL 8, PostgreSQL 12
- 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.
- Official documentation
- PostgreSQL documentation, The SQL Language
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 "This query is slow and also misses rows: WHERE LOWER(email) = LOWER(?) AND...": https://scrivio-interview-library.netlify.app/databases/sql-fundamentals/f373a653ea/