When to use single quotes, double quotes, and backticks in MySQL

When to use single quotes, double quotes, and backticks in MySQL

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

When to use single quotes, double quotes, and backticks in MySQL

Consistency matters in codebases, but consistency without understanding the underlying semantics is just a ritual that invites bugs. In MySQL, the distinction between single quotes ('), double quotes ("), and backticks (`) isn't merely stylistic; it dictates how the engine parses your schema and data. Mixing them arbitrarily can lead to unexpected behavior, from silent data truncation to runtime errors that are difficult to debug.

Single Quotes for String Literals

Single quotes are the standard delimiter for string literals in SQL. They tell the MySQL parser that everything inside them is a value, not a command or an identifier. This applies regardless of whether you are inserting static text like 'Hello World' or dynamic data coming from a variable.

In the context of your example:

INSERT INTO table (id, col1, col2) VALUES (NULL, val1, val2);

If val1 and val2 are variables containing user input, they must be wrapped in single quotes when you construct the query string. The parser treats 'user@example.com' as a text value. If you omit the quotes or switch to double quotes for this specific content, MySQL will attempt to interpret those words as column names first.

A common pitfall occurs when dealing with strings that contain single quotes themselves, such as an address: "O'Brien Street". To include this safely in a query, you must escape the internal quote using a backslash or by doubling it:

'He said ''Hello'' to me' -- Interpreted as: He said 'Hello' to me

This escaping rule is critical for preventing SQL injection vulnerabilities. Even though modern frameworks like Laravel handle parameter binding automatically, understanding this mechanism is essential when writing raw queries or debugging third-party scripts.

Double Quotes for Identifiers (with caveats)

Double quotes are reserved for identifiers—table names, column names, and database names—but only under specific conditions. MySQL allows the use of double quotes as delimiters for identifiers in non-standard SQL mode (the default since version 8.0.13). However, they are strictly interpreted as case-sensitive aliases when used with backticks or when the ANSI_QUOTES SQL mode is enabled.

If you see a query like "users" or "created_at", be aware that MySQL treats this as a specific identifier name. If your table is actually named Users (capital U), and you write "users", MySQL will look for a table literally named users. In standard mode without ANSI_QUOTES, double quotes are often treated loosely, but relying on this behavior creates portability issues with other SQL dialects like PostgreSQL or Oracle, where double quotes always mean case-sensitive identifiers.

The safest practice is to stick to backticks for identifiers if you need case sensitivity or special characters, reserving double quotes for cases where you specifically require ANSI-compliant case-sensitive naming conventions.

Backticks for Special Characters and Case Sensitivity

Backticks (`) are the most robust delimiter for identifiers in MySQL. They wrap table names, column names, and database names to protect them from SQL keywords and special characters. For instance, if you have a table named Order (a reserved keyword), you must write it as `Order` in your query:

SELECT * FROM `Order`;

Without the backticks, MySQL would interpret Order as a command rather than a table name, resulting in a syntax error.

Backticks are also necessary when identifiers contain spaces or dots that aren't part of a schema path. If you have a column named first name, using single quotes here would define it as a string value, not a column reference:

SELECT `first name` FROM users; -- Correct for identifier with space
SELECT 'first name' FROM users;  -- Incorrect: treats 'first' as a column and 'name' as literal text (or vice versa depending on context)

Dynamic Queries and Variables

Returning to your original scenario where table, col1, and val1 are variables, the rule of thumb remains strict. Never concatenate raw user input into an identifier position without validation. If you must construct dynamic queries using variables for table or column names (which should be whitelisted against a list of safe identifiers), always use backticks to ensure the parser treats them as identifiers rather than string literals.

// Dangerous: assumes $table is validated, but syntax is fragile if not careful
$query = "SELECT * FROM {$table} WHERE `col1` = '{$val1}'"; 

// Safer approach using prepared statements for values
$stmt = $connection->statement("SELECT * FROM {$table} WHERE col1 = ?", [$val1]);

In this pattern, the table name comes from a trusted source (hence it doesn't need quotes in the final string construction logic if passed as an argument to the query builder), while the value val1 receives single quotes via parameter binding. Mixing double quotes for identifiers and backticks for values is generally discouraged; stick to backticks for schema elements and single quotes for data to maintain clarity and prevent accidental scope confusion.

Our network