What is SQL injection?
SQL injection is an attack where someone types specially crafted text into a website, and the site passes it to its database as part of a command. The database then runs the attacker's command instead of the one the developer wrote. It can let attackers read, change or delete data they should never touch.
SQL injection is part of A05:2025 Injection in the current OWASP Top 10, the best-known list of web app risks. Its formal ID is CWE-89 (CWE is a public catalog of software flaw types). Injection dropped from third to fifth place in the 2025 list, but it still has more published flaws than any other category.
Databases and queries in plain words
Most apps keep their data in a database: customers, orders, passwords, invoices. Think of it as a set of very large spreadsheets.
To get data out, the app sends the database a question written in SQL (Structured Query Language). That question is called a query. For example, "find the user with this email address" becomes a short line of SQL.
The problem starts when the app builds that query by gluing the user's text straight into it. The database cannot tell which part the developer wrote and which part came from the user. If the user's text contains SQL, the database runs it.
A simple SQL injection example: the login check
Here is a simplified login check. It is shortened for teaching: real apps should compare a password hash (a one-way scramble of the password), not the password itself.
Unsafe pattern: query = "SELECT * FROM users WHERE email = '" + email + "' AND password = '" + password + "'"
The app takes whatever was typed into the email box and drops it between quote marks. Now picture a visitor who types an apostrophe. That apostrophe closes the quote early. Anything typed after it is read as SQL, not as an email address.
So an attacker can add a condition that is always true, or cut off the password check. The database then says "yes, this user exists", and the attacker is logged in, often as the first account in the table. That first account is frequently an admin.
Safe pattern: db.query("SELECT * FROM users WHERE email = ?", [email])
The question mark is a placeholder. The query and the user's text travel to the database separately. Whatever the user types, even apostrophes and SQL words, is treated as plain data. It can never change the shape of the command.
Types of SQL injection
Testers usually group SQL injection into three types. The difference is how the attacker sees the results.
- Classic (in-band): the results come straight back on the page. For example, a product search shows customer records instead of products, or an error message reveals table names.
- Blind: the page shows no data, but it behaves differently. The attacker asks yes or no questions, such as "does the admin password start with A?", and watches whether the page changes or loads more slowly. It is slow, but tools can automate it.
- Out-of-band: the attacker makes the database send data to a server they control, for example through a web or DNS request (DNS is the internet's address book). It only works when the database is allowed to reach the internet.
Blind SQL injection is easy to miss. The app looks fine to a normal user, so only careful testing finds it.
What an SQL injection attack can do
The damage depends on what the database account is allowed to do. Common results include:
- Logging in as another user, including an admin, without a password.
- Reading every row in the database: customer details, password hashes, payment records.
- Changing data, such as prices, balances or user roles.
- Deleting tables and taking the app offline.
- On some database setups, running commands on the server itself.
Any of these can turn into a reportable data breach, with legal and customer trust costs on top of the cleanup.
How to prevent SQL injection
The good news: SQL injection is one of the best-understood flaws, and the fixes are well known. Use them in layers.
- Use parameterized queries everywhere. Also called prepared statements. This is the main fix, as shown above. OWASP lists it as the first defense and strongly discourages escaping input by hand instead.
- Use your ORM safely. An ORM is a library that writes SQL for you, such as Hibernate, Entity Framework, Prisma or Django's ORM. Its normal methods use parameters. But most ORMs also offer a "raw query" option. If developers paste user text into a raw query, the risk comes straight back.
- Give the database account the least access it needs. The app's account should never be a database admin. If it only needs to read products, it should not be able to drop tables. This limits the damage if something slips through.
- Validate input as a second layer. Check that an order number is a number and a country code is on your list. This helps, but OWASP is clear that it does not replace parameterized queries.
- Add a WAF as a backstop. A WAF (web application firewall) is a filter in front of your site that blocks known attack patterns. It can buy time, but attackers often find ways around it. Treat it as a safety net, not the fix.
For the wider checklist your web team should follow, see our web security best practices.
How pentesters test for SQL injection
A penetration test (pentest) is an authorized attempt to break into your app so you can fix gaps first. For SQL injection, a tester will typically:
- Map every input: forms, web address parameters, cookies, headers and API fields (an API is how apps talk to each other).
- Send characters that have special meaning in SQL and watch for errors or changes in behavior.
- Compare true and false conditions, and response times, to find blind injection.
- Confirm the flaw with the least intrusive proof, without changing or deleting your data.
- Report the exact request, the risk and the code-level fix, then retest once you have fixed it.
Automated scanners find many simple cases. A skilled tester finds the rest, such as injection in an API field or a rarely used admin report. The same input-handling mistakes also cause cross-site scripting (XSS), so good tests check for both.
How NextGarrison helps
NextGarrison runs web app and API penetration tests that check every input for SQL injection and the other OWASP Top 10 risks. You get clear findings, ranked by real risk, with fixes your developers can apply, and a retest to confirm they work. See our penetration testing services or ask for a scoping call.
Sources

Need a hand with this?
Talk to a senior consultant. We reply within one business day with clear next steps and a scoped quote.
Prefer to just ask? Contact us and a consultant will reply.
Written by
Vince AntonioSecurity writer and consultant, NextGarrison
Vince writes NextGarrison's security news, technical vulnerability write-ups and guides. He covers actively exploited flaws, penetration testing, SOC monitoring and getting ready for SOC 2, ISO 27001 and PCI DSS, in plain English with sources for every claim.
Want the next alert like this one?
New vulnerability alerts as soon as we publish them. Free, no sign-up, works in Feedly, Outlook, Slack and Teams.
