SQL injection (SQLi) is one of the oldest and most damaging web vulnerabilities. It happens when user input is placed directly into a database query, so an attacker can change what the query does.
How SQL injection works
Imagine a login form that builds this query by joining strings:
SELECT * FROM users WHERE name = 'input' AND password = 'input'
If the application does not separate data from code, specially crafted input can change the logic of the query, for example making the WHERE condition always true and bypassing the login. This is why SQL injection belongs to the Injection category of the OWASP Top 10.
Types of SQL injection
- In-band: results come back in the same response (error-based or UNION-based).
- Blind: no data is shown, so the attacker infers it from true/false responses (boolean-based) or delays (time-based).
- Out-of-band: data is sent to another channel such as DNS.
Impact
Data theft, login bypass, modification or deletion of records, and in some configurations full server compromise.
How to prevent SQL injection
- Use parameterised queries (prepared statements) everywhere. This is the primary fix because the database treats input strictly as data.
- Use safe ORM methods and avoid building SQL strings by hand.
- Validate input with allow-lists (for example, IDs must be numbers).
- Give the application’s database account the least privilege it needs.
- Hide detailed database errors from users and log them instead.
- Add a web application firewall as an extra layer, not as the main fix.
Testing ethically
Only test applications you own or are explicitly authorised to test, such as in-scope targets of a bug bounty programme or deliberately vulnerable practice apps.
Learn more
The Bug Bounty & Web Application Security course covers SQLi, XSS, IDOR, SSRF and the rest of the OWASP Top 10.
Frequently asked questions
What is SQL injection?
A vulnerability where user input is inserted into a database query without proper separation, letting an attacker change the query.
What is the best way to prevent SQL injection?
Use parameterised queries (prepared statements) for every database query, supported by input validation and least-privilege database accounts.
What is blind SQL injection?
A type of SQL injection where no data is returned directly, so the attacker infers information from true/false responses or time delays.
Learn this topic properly
Scope In Science has theory notes and chapter-wise question banks with answers for Physics, Chemistry, Maths, Biology and Cybersecurity, in 18+ languages. Start with the free lessons and practice questions.