Learn SQL
-- security.injection_parameters

SQL Injection & Parameters

Input becomes code — placeholders stop it

--concept

Concatenating untrusted input into SQL lets attackers alter structure (' OR '1'='1, stacked queries, UNION exfiltration). Defense: parameterized queries/prepared statements (values never parse as code), least-privilege roles, validation as depth. ORMs still inject through raw fragments — parameterization is the rule at every layer.

--how it works
1

Concatenation is the vuln

Input merged into SQL text re-parses as keywords — quotes become syntax.

2

Classic widen + exfiltrate

' OR '1'='1 widens to all rows; UNION appends other-table dumps.

3

Parameterize everything

Placeholders separate structure from values — values never parse.

4

Depth + review

Least-privilege roles, validation, logging; flag any string-built SQL in review.

--visualization
Structure + values separate — quotes stay data
rows
1 exact match
Stage 1 / 1
Placeholders never re-parse values as keywords. Flag string-built SQL in review.
--example
query.sql
1-- SAFE (parameter, never formatted in):
2-- cur.execute("SELECT * FROM users WHERE name = %s", (name,))
Result
styleverdict
interpolationvulnerable — input parses as code
parametersafe — value never parses

Placeholders send structure and values separately — the engine never re-parses values as keywords. String building merges them, so quotes become syntax.

--variants

Widening attack shape

One quote changes the predicate to TRUE.

query.sql
1-- input: ' OR '1'='1
2-- built: SELECT * FROM users WHERE name = '' OR '1'='1'; -- all rows!
Result
styleresult
interpolatedall rows leak
1Closing quote ends the literal early.
2OR TRUE widens to every row.
3Login/role checks bypassed with no error.

Parameterized fix

Structure and values travel separately.

query.sql
1-- cur.execute("SELECT * FROM users WHERE name = %s", (name,))
Result
styleresult
parameterizedexact-name match only
1Placeholder typed by engine, not stringified.
2Quote stays data — never syntax.
3ORM raw() fragments need the same discipline.
--key facts
Placeholdersonly fix — never interpolate
ORMsraw fragments still inject
Depthleast privilege + validation + logging
--use cases
Login/search endpoints with untrusted input
ORM audits: raw fragments + report builders
--under the hood
Prepared statements plan once, bind many — injection-proof plus faster hot loops.
Least-privilege roles bound blast radius when a hole slips through.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.