How to escape apostrophe a single quote in 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 string literals, a single quote ends the string, so an apostrophe inside the text has to be handled correctly. The SQL-level answer is to escape it inside the literal, but the application-level answer is usually not to build that literal by hand at all. In real code, parameterized queries are safer and simpler than manual escaping.
SQL Literal Syntax: Double the Quote
The portable SQL way to include a single quote inside a string literal is to double it.
That produces the text O'Brien. The doubled quote does not mean two apostrophes in the result. It means one apostrophe inside the SQL string literal.
The same rule applies in INSERT and UPDATE statements:
If you are writing raw SQL manually, this is the standard SQL-compatible way to represent the character.
MySQL Also Supports Backslash Escaping in Many Setups
In MySQL, you will also see backslash escaping:
This often works, but it depends on SQL mode. If the server runs with NO_BACKSLASH_ESCAPES, backslash is no longer treated as an escape character in string literals.
That is why doubled quotes are usually the safer SQL-level recommendation. They are standard and more portable across databases.
Best Practice: Use Parameters Instead of Manual Escaping
In application code, you should usually avoid building SQL strings with embedded values. Let the database driver handle quoting and escaping through parameters.
Python example:
This is better than manually escaping the apostrophe because:
- it avoids SQL injection risks
- it keeps SQL readable
- it lets the driver handle the database-specific quoting rules
If the value contains apostrophes, backslashes, or other special characters, the driver deals with them correctly.
Why Manual String Building Is Dangerous
Apostrophes are not the only problem. Once code starts concatenating SQL strings, every user-controlled value becomes a security concern.
Bad pattern:
This breaks as soon as the string contains a quote, and it also opens the door to injection attacks if the input is malicious. Escaping by hand tends to grow into fragile code very quickly.
Literal Escaping Still Matters in Direct SQL Tools
Even though parameterization is the application-level best practice, literal escaping is still useful when:
- writing one-off SQL in a MySQL shell
- building migration scripts
- debugging with ad hoc statements
In those contexts, doubling the quote is the cleanest habit:
That keeps the intent obvious and does not depend on special MySQL backslash behavior.
Character Sets Are a Separate Concern
Escaping an apostrophe is about SQL syntax, not text encoding. If you see mojibake or broken Unicode characters, the problem is probably client encoding, connection collation, or table charset, not single-quote escaping.
It helps to separate these concerns:
- apostrophe escaping fixes parser syntax
- character set configuration fixes text encoding
Mixing them together makes debugging harder.
Common Pitfalls
- Building SQL strings by concatenation and trying to escape quotes by hand.
- Assuming backslash escaping always works regardless of MySQL SQL mode.
- Forgetting that the SQL-standard escape form is doubled single quotes.
- Treating quote escaping as sufficient protection against SQL injection.
- Confusing syntax escaping with character encoding issues.
Summary
- In SQL literals, a single quote inside the text is commonly written as two single quotes.
- MySQL often supports backslash escaping too, but doubled quotes are more portable and reliable.
- In application code, parameterized queries are usually the correct solution.
- Avoid manual SQL string concatenation whenever possible.
- Separate quote-escaping problems from character-encoding problems when debugging.
Related reading
- How to escape single quotes in Unload
- How to establish a connection to DynamoDB using python using boto3
- How to exclude some tables from RDS Mysql replication
- How to execute a MySQL command from a shell script?
- How to execute a sql script file in a Kubernetes Pod?
- How to execute an SSIS package from .NET?
- How to execute IN SQL queries with Spring's JDBCTemplate effectively?
- How to execute MySQL command from the host to container running MySQL server?

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.