SQL
MySQL
string comparison
case sensitivity
database

How can I make SQL case sensitive string comparison on MySQL?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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:

sql
SELECT 'Alice' = 'alice';

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.

sql
SELECT *
FROM users
WHERE username COLLATE utf8mb4_0900_as_cs = 'Alice';

For a binary comparison, you can also use utf8mb4_bin:

sql
SELECT *
FROM users
WHERE username COLLATE utf8mb4_bin = 'Alice';

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.

sql
SELECT *
FROM users
WHERE BINARY username = 'Alice';

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.

sql
1CREATE TABLE users (
2    id INT PRIMARY KEY AUTO_INCREMENT,
3    username VARCHAR(100) COLLATE utf8mb4_0900_as_cs NOT NULL
4);

Or change an existing column:

sql
ALTER TABLE users
MODIFY username VARCHAR(100) COLLATE utf8mb4_0900_as_cs NOT NULL;

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.

sql
SELECT *
FROM users
WHERE username COLLATE utf8mb4_0900_as_cs LIKE 'Al%';

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_cs or COLLATE ... bin for query-time case-sensitive comparisons.
  • 'BINARY also 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.

Course illustration
Course illustration

All Rights Reserved.