SQL Injection and Parameterised Queries

What goes wrong when data is pasted into a query string, and the one fix that actually works.

SQL Injection and Parameterised Queries

The same lookup, four ways

query.sql SQLite
Result

Worth knowing

Injection happens when input is concatenated into SQL, so the database cannot tell data from instructions.
A parameterised query sends the SQL and the values separately. The value can never become syntax, whatever it contains.
Escaping is not the fix. It is a blacklist, it varies by engine and charset, and it fails on numeric contexts entirely.
Identifiers — table and column names — cannot be parameterised. Those need an allowlist.

Overview

If an application builds SQL by concatenating user text into a query, special characters can change what the database executes. An attacker may bypass a login check, read data, or alter rows depending on the query and permissions. Escaping strings by hand is error-prone because quoting rules differ across drivers and contexts.

In this guide
  1. Bind values as parameters
  2. Use least privilege too
  3. Separate trusted structure from user choice
  4. Test the query boundary
  5. Further reading

Bind values as parameters

Use the database driver's parameterized-query API. The SQL statement contains placeholders, and the driver sends user values separately as data.

query.sqlSQLite
Result

The placeholder syntax varies by driver. Bind the supplied email through that API instead of inserting it into the SQL string. Parameterization also lets the database reuse query plans in some systems, but the security property is the separation of values from SQL code.

Parameters represent values, not SQL identifiers or keywords. If a user can choose a sort column or table, map that choice through a strict allowlist of known identifiers and construct the query from that trusted mapping. Never treat arbitrary input as a column name.

Use least privilege too

Parameterization prevents a class of injection, but it does not replace authorization, validation, or database permissions. Give the application account only the access it needs, avoid exposing detailed database errors to users, and keep dependencies current. See DML for safe row changes.

Separate trusted structure from user choice

For a sort option, map a small public choice such as “newest” to a fixed SQL fragment such as created_at DESC. Do not bind the string as though a value placeholder could stand for the column name, and do not concatenate an arbitrary request parameter. Bind the actual page size and filter values separately. Review every query-building path, including search, exports, and administrative endpoints; one safe login query does not protect the rest of the application.

Test the query boundary

A useful security test supplies quotes, comment markers, wildcard characters, and Unicode in fields that are meant to be ordinary text. The value should remain data and must not change the query's meaning. Parameterization does not decide whether that value is authorized or valid, so enforce length, format, and ownership rules separately. Review logs and error paths so a rejected input does not disclose schema details. Treat each database driver’s placeholder syntax as part of its API and avoid mixing string formatting with bound values.

Further reading

Check yourself

0 of 3

Answer without scrolling back up.

  1. Why does a parameterised query prevent injection?

  2. Why is escaping an inadequate defence?

  3. How should a user-supplied ORDER BY column be handled?

Cheat sheet

SQL Injection and Parameterised Queries

Build a query with string concatenation, inject a payload that drops a table, then switch to a parameterised query that blocks it completely.

DATABASE · vizlearn.in/database/sql_injection_and_parameters.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.