Untitled Lesson
Skill Matrix
UNLOCK NODES BY LEARNING NEW TAGS.
Why does a parameterized query prevent SQL injection, even if the user input contains characters like a single quote (')?
💻 Code Challenge | +75 XP
Rewrite a vulnerable login query that concatenates username and password directly into a SQL string, converting it to a parameterized query.
A dynamic product-sorting feature accepting a "sortBy" query parameter was found to be exploitable for SQL injection. Reorder the steps to fix it correctly.
Task: Reorder the blocks in logical sequence to solve the problem.
A.D.A. Interface
Adaptive Didactic Assistant

Pascual Vila
Frontend Instructor // Code Syllabus
The Error //
Using an ORM's raw query escape hatch with string-concatenated user input
// Wrong: raw query escape hatch, unsafe concatenation
await sequelize.query(`SELECT * FROM users WHERE id = ${id}`);
// Correct: parameterized even in raw queries
await sequelize.query("SELECT * FROM users WHERE id = ?", { replacements: [id] });The Solution //
ORMs parameterize their standard query builder methods automatically, but raw/literal query escape hatches bypass that protection entirely — concatenating user input into one reintroduces the exact same SQL injection vulnerability the ORM otherwise prevents.
The Error //
Attempting to parameterize a dynamic column or table name the same way as a value
// Wrong: parameterization doesn't work for column names
db.query("SELECT * FROM t ORDER BY $1", [sortColumn]); // often fails or is ignored
// Correct: allowlist validation
if (!["name", "date"].includes(sortColumn)) throw new Error("Invalid column");The Solution //
Database drivers only support parameterization for values, not for structural query elements like column names, table names, or ORDER BY direction — attempting to bind these as parameters either fails outright or is silently ignored. Validate these against a strict allowlist of known-safe options instead.