SQL Injection
& Mitigations
Learn how attackers exploit unsanitized database queries and how to defend against them with parameterized queries and secure coding practices.
What is SQL?
Structured Query Language (SQL) is the standard language for interacting with relational databases. Web applications use SQL to read, write, update, and delete data stored in tables.
What is SQL Injection?
SQL Injection (SQLi) is an attack where a malicious user inserts harmful SQL code into an input field. If the application builds queries by concatenating user input directly, the database executes the attacker's code as if it were legitimate.
Why Should You Care?
How SQL Injection Works
The Vulnerable Query
If a user types ' OR '1'='1' -- as the username, the query becomes:
Types of SQL Injection
Data vs. Code
The fundamental flaw behind SQL injection is a confusion of boundaries. The database engine cannot distinguish between what the developer intended as structure and what the user supplied as content.
| Safe (Data) | Dangerous (Code) | |
|---|---|---|
| User types | alice | ' OR '1'='1 |
| DB interprets as | A string value | SQL logic (always true) |
| Result | Returns alice's record | Returns all records |
| Intent | Legitimate lookup | Authentication bypass |
The Fix: Parameterized Queries
A parameterized query (also called a prepared statement) separates the SQL structure from the data. The database engine compiles the query template first, then binds user input strictly as data values.
How Parameterization Protects You
How to Defend Against SQLi
Example 1: Login Bypass
A login form asks for a username and password. The server builds this query:
An attacker enters admin' -- as the username. The -- is a SQL comment, so everything after it is ignored:
Example 2: UNION-Based Data Theft
A product search page runs this query with the user's search term:
The attacker searches for: ' UNION SELECT username, password FROM users --
Example 3: Destructive Payload
The same vulnerable search field can be exploited to destroy data:
The server constructs:
Fixing Each Example
Common Mistakes
Defense-in-Depth Summary
| Defense Layer | What It Does | Strength |
|---|---|---|
| Parameterized Queries | Separates SQL code from data | ★★★ Primary |
| Input Validation | Rejects unexpected formats | ★★ Secondary |
| Least Privilege | Limits DB account permissions | ★★ Secondary |
| ORM / Query Builder | Auto-parameterizes queries | ★★★ Primary |
| WAF | Blocks known attack patterns | ★ Supplementary |
| Error Handling | Hides DB error details from users | ★ Supplementary |
Question 1 of 5
What is the root cause of SQL injection vulnerabilities?
Question 2 of 5
An attacker enters ' OR '1'='1 into a login form. What does this payload do?
Question 3 of 5
Which defense is considered the primary mitigation against SQL injection?
Question 4 of 5
In a UNION-based SQL injection, what does the attacker accomplish?
Question 5 of 5
Why is manually escaping single quotes considered an unreliable defense against SQL injection?