Intermediate Topic 24 of 51 Security & Cryptography
SQL Injection
Attack inserting malicious SQL through user input.
How it works
SQL injection occurs when an application builds a database query by inserting untrusted input into SQL text, letting an attacker change the query's structure, for example by supplying input such as a quote followed by an OR condition that is always true. Consequences include reading or modifying any data, bypassing login, and sometimes executing commands on the server. Variants include in-band, blind and out-of-band injection.
Step-by-step walkthrough
- The app builds SQL with string concatenation.
- The attacker types ' OR '1'='1.
- The query becomes always true.
- The database returns all rows.
- The fix is parameterized queries, which keep input as data.
Where it is used
Understanding it matters for every developer writing database code. The defense is parameterized queries or prepared statements, which keep data separate from code, with ORMs that use them by default.
Common pitfall
String concatenation and escaping by hand are fragile. Dynamic parts such as table names or ORDER BY columns cannot be parameterized and must be validated against an allow-list. Stored procedures built from concatenated strings remain vulnerable. Apply least privilege to database accounts, hide detailed error messages and use web application firewalls only as an extra layer. Test with security scanners and code review.
Where it fits in the course
SQL Injection is topic 24 of 51 in Security & Cryptography, rated Intermediate. It follows Message Authentication Codes. The next topic is Cross-Site Scripting.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Security & Cryptography quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Security & Cryptography quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is SQL Injection?
Where do people use SQL Injection?
What is the most common mistake with SQL Injection?
What should I learn before this topic?
How can I practice it?
Is SQL Injection hard to learn?
How long does it take to learn SQL Injection?
Which topics are related to SQL Injection?
What should I study after SQL Injection?
Related topics
- Message Authentication Codes (Security & Cryptography)
- Cross-Site Scripting (Security & Cryptography)
- Block Cipher Modes (Security & Cryptography)
- CSRF (Security & Cryptography)
- Dependency Injection (Software Engineering)