Skip to content
IT glossary
SecurityUpdated December 3, 2026

SQL injection

SQL-инъекция

SQL injection is a flaw that lets input change a database command instead of being treated only as data. It can make an application read or change records it was not supposed to access.

What goes wrong

Imagine a support tool that looks up customers by last name. The database should use the name only as the value to search for. If the application builds the SQL text by inserting that name into a string, the database parses the entire result as SQL. It has no way to tell which text came from the developer and which came from the form. SQL injection is possible when input at that boundary can change what the command means.

Keep values separate from SQL

Use the parameter binding API provided by the database driver. With Python's sqlite3 module, the SQL statement contains a ? placeholder, and the value goes in a separate argument:

cursor.execute(
    "SELECT id, last_name FROM customers WHERE last_name = ?",
    (last_name,),
)

A name like O'Connor is still one value; its apostrophe cannot change the query's structure. Placeholder syntax varies by driver, so use the driver's documented API rather than substituting values with a language's string formatting features.

What to check in a code review

Start where SQL is built dynamically: search filters, report builders, background jobs, and hand-written queries passed through an ORM. Trace where the values came from, not just where the query runs. A customer preference saved safely yesterday can still be unsafe if today's reporting job copies it into SQL text.

Database permissions limit the damage if a flaw slips through. For example, a reporting account with read-only access cannot modify records, but an unsafe query can still reveal data that account is allowed to read. Least privilege complements parameter binding; it does not replace it. Likewise, validating input for type or length is useful, but it does not keep input separate from SQL.

How it comes up in interviews

A common follow-up is how to let users choose a sort order. A bound parameter supplies a value; it doesn't select a column or stand in for the DESC keyword. Map supported options to fixed SQL fragments, reject unknown options, and keep binding filter values separately. A strong answer explains not only how to fix a query, but also which parts of it the caller should never control directly.