Application security SQL injection 4 min read

What is SQL injection?

A sign-in form pastes whatever you type into its database query. Someone types ' OR '1'='1 as their name. What does the database go looking for?

  1. Pasted input becomes part of the command

    The app builds its query like this: "SELECT * FROM users WHERE name = '" + name + "'". With that input, the database receives SELECT * FROM users WHERE name = '' OR '1'='1'.

    The quote in the input closed the string early, and '1'='1' is always true. The input stopped being a name and became part of the command, so the query matches every user.

    Someone types a quote followed by OR 1 equals 1. The app pastes it into its query text and sends it to the database, which reads it as a condition that is always true and returns every user.

    The database can’t tell which part of the text you meant as data.

    In short When input is pasted into query text, it can change what the query means.

  2. Send the query and the data separately

    A parameterised query sends the command with a placeholder, and the input alongside it: db.query("SELECT * FROM users WHERE name = ?", [name]). The database fills the placeholder in as data, whatever it contains.

    ORMs do this for you. Formatted strings like f"... '{name}'" don’t: that’s still pasting.

    A query string where the part that came from the user, quote OR quote one equals one, is highlighted in red. A second version shows the input in its own separate box marked data only. pasted into the queryWHERE name = '' OR '1'='1'sent as a parameterWHERE name = ?' OR '1'='1data only
    Pasted, the input joins the command. As a parameter, it stays in its own box.

    In short A parameter can only ever be data. It can’t become a command.

  3. Why filters aren’t the fix

    Blocking quotes or words like OR seems quicker. But filters get bypassed with tricks you didn’t think of, and they break real names like O’Brien. Escaping quotes by hand misses cases too.

    Hiding database errors doesn’t close the hole either. It just makes it a little harder to find.

    In short Fix the cause, not the symptoms: use parameters everywhere.

Where parameters can’t help

Parameters are for values, not names. A table you can sort by any column, built as "... ORDER BY " + sort, can’t pass sort as a parameter.

Check it against a fixed list instead, such as name, date and total, and refuse anything else.

Keep this

Never paste input into a query. Pass it as a parameter, so it can only ever be data.

Filters and escaping miss cases. Parameters don’t. For things that can’t be parameters, like column names, allow only known values.
Cause
Input pasted into the query text.
Effect
The input can change what the query does.
Fix
Parameterised queries keep input as data.
Edge case
Column names can’t be parameters, so allow-list them.

Questions worth carrying

Do ORMs make me safe automatically?

Mostly, as long as you use their normal query methods. Most ORMs also have a “raw SQL” escape hatch, and pasting input there brings the problem straight back.

Is NoSQL safe from injection?

No. The details differ, but the cause is the same: input treated as part of a query. Keep input as values.

Sources

Last reviewed 17 November 2026.