MySQL SELECT WHERE datetime matches day and not necessarily time
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Filtering rows by day in a DATETIME column is a common SQL task, but many queries become slow or incorrect when time and time zone details are ignored. The key is to separate correctness from performance and design a predicate that gives both. This guide explains practical query patterns, why some are faster, and how to avoid hidden date bugs.
Why Day Matching Is Tricky
A DATETIME value includes both date and clock time. If you compare directly to a date string, only rows at midnight will match exactly.
Most rows have non-zero time, so direct equality is rarely what you want.
Correct and Fast Pattern: Half-Open Range
The most reliable approach is a half-open interval from the start of day to the start of next day.
This pattern has two major advantages:
- It is exact for the full day.
- It is index friendly on
created_at.
When an index exists, MySQL can use range scans efficiently.
Convenient but Slower Pattern: DATE()
You can also wrap the column in DATE().
This is readable, but often slower on large tables because function wrapping can prevent index range use on the original column. It may still be acceptable for small datasets or ad hoc analysis, but it is usually not ideal for hot paths.
Parameterized Query Pattern from Application Code
In applications, build day boundaries once and pass them as parameters.
This keeps SQL safe and reusable, and it avoids string concatenation mistakes.
Time Zone Considerations
If values are stored in UTC but users filter by local day, translate the requested local day to UTC boundaries before querying. For example, a local date in Toronto does not always map to midnight UTC because of offset and daylight savings changes.
Practical workflow:
- Convert user local day start to UTC.
- Convert next local day start to UTC.
- Query with the same half-open range in UTC.
This avoids ambiguous local timestamps during daylight savings transitions.
Generated Columns for Heavy Analytics
If day-level filtering is extremely frequent, consider a generated date column and index it.
This can provide convenient syntax with strong performance, but it adds storage and schema complexity. Use only if profiling justifies it.
Common Pitfalls
Using DATE(created_at) in every production query is a common performance mistake. It looks simple but may force full scans as data grows. Prefer boundary ranges for primary paths.
Another issue is mixing local date strings with UTC-stored data. This creates off-by-one-day bugs near midnight and daylight savings boundaries. Normalize date boundaries to one time zone before querying.
Developers also misuse BETWEEN with full timestamps and accidentally include next-day midnight. Half-open range logic is usually clearer and less error-prone.
Finally, avoid dynamic SQL date string interpolation. Always use bound parameters to prevent formatting issues and security risks.
Summary
- Use a half-open day range for both correctness and index efficiency.
- Keep an index on the
DATETIMEcolumn for fast scans. - Use
DATE()only when convenience outweighs performance concerns. - Convert local day boundaries to the storage time zone before querying.
- Use generated date columns only for proven high-frequency day filtering needs.
Related reading
- MySQL selecting rows where a column is null
- MySQL selecting yesterday's date
- MySQL Server has gone away when importing large sql file
- MySQL server startup error 'The server quit without updating PID file
- MySQL sharding and partition in distributed system
- MySQL show current connection info
- MySQL show status - active or total connections?
- MySQL skip first 10 results

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.