How can I make SQL case sensitive string comparison on MySQL?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In MySQL, case sensitivity is controlled primarily by collation, not by the = operator itself. If your column uses a case-insensitive collation, then string comparisons such as = and LIKE are usually case-insensitive by default.
That means the correct fix is usually not “find a different operator.” The correct fix is to use a case-sensitive or binary collation for the comparison you want, either in the query or in the schema.
Why Comparisons Are Often Case-Insensitive
For nonbinary string types such as CHAR, VARCHAR, and TEXT, MySQL compares values according to the collation of the operands. In current MySQL 8 defaults, many databases use utf8mb4 with a case-insensitive collation such as utf8mb4_0900_ai_ci.
So a comparison like this may treat Alice and alice as equal:
Whether that returns true depends on the effective collation. If the collation is case-insensitive, the comparison is case-insensitive too.
Use COLLATE for a One-Off Case-Sensitive Comparison
The cleanest query-time solution is to apply a case-sensitive collation in the comparison itself.
For a binary comparison, you can also use utf8mb4_bin:
These are useful when you want only one query to behave case-sensitively without changing the table definition.
Use BINARY for Bytewise Comparison
Another common technique is to cast one side to a binary string with BINARY.
This forces a binary comparison, which is case-sensitive because it compares byte values rather than using a case-insensitive collation.
This style is concise and often seen in answers and legacy code. It works, but COLLATE is usually more explicit about the intended comparison rules.
Make the Column Case-Sensitive Permanently
If a column should always behave case-sensitively, set its collation accordingly.
Or change an existing column:
This is appropriate when case sensitivity is a property of the domain itself, not just one query. Usernames, codes, and certain identifiers sometimes fall into that category.
LIKE Is Affected Too
The same rule applies to LIKE because it also respects collation for nonbinary strings.
If the collation is case-sensitive, LIKE 'Al%' and LIKE 'al%' are different searches.
Choosing Between as_cs and bin
There are two common choices:
- a case-sensitive collation such as
utf8mb4_0900_as_cs - a binary collation such as
utf8mb4_bin
The first follows character collation rules while distinguishing case. The second compares raw byte values. If you simply want “uppercase and lowercase should not match,” both may work, but they are not conceptually identical.
For ordinary text data, a case-sensitive collation is often the more semantically precise choice. For strict bytewise matching, use a binary collation or BINARY.
Common Pitfalls
A common mistake is assuming MySQL string comparison is always case-insensitive. It depends on the collation in effect.
Another mistake is changing the whole column collation when you only needed one query to be case-sensitive. That can affect sorting, indexing behavior, and application assumptions.
Developers also confuse case sensitivity with accent sensitivity. A collation name such as ai_ci and as_cs encodes multiple comparison rules, not just case handling.
Finally, do not use LOWER() or UPPER() to force behavior unless that is truly your intended logic. Those functions change the expression and can make index usage less straightforward.
Summary
- MySQL string case sensitivity is controlled by collation for nonbinary strings.
- Use
COLLATE ... as_csorCOLLATE ... binfor query-time case-sensitive comparisons. - '
BINARYalso forces a case-sensitive bytewise comparison.' - Change the column collation only if the column should always be case-sensitive.
- Decide whether you want character-aware case sensitivity or strict binary matching before choosing the approach.
Related reading
- How can I modify the size of column in a MySQL table?
- How can i optimize MySQL's ORDER BY RAND function?
- How can I output MySQL query results in CSV format?
- How can I output MySQL query results in CSV format?
- How can I pass environment variables to mongo docker-entrypoint-initdb.d?
- How can I prevent SQL injection in PHP?
- How can I provide different database configurations with Spring Boot?
- How can I put a database under git version control?

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.