SQL injection is an attack in which untrusted input becomes part of a database query, so the database runs the attacker's SQL as well as the developer's. It happens when an application builds queries by joining user input into SQL text instead of using parameterized queries. A successful attack can read, change or delete data, bypass logins and, in some cases, take over the server.
Key takeaways
- SQL injection happens when user input is joined into SQL text instead of being bound as data through parameterized queries.
- It ranked second on MITRE's 2025 CWE Top 25 list of the most dangerous software weaknesses.
- Parameterized queries, least-privilege database accounts and testing are the defenses that actually stop it.
SQL injection is one of the oldest attacks on the web, and it refuses to die. It lets an attacker type database commands into an ordinary input box and have your application run them — no stolen password required.
What is SQL injection?
SQL injection is an attack in which untrusted input becomes part of a database query, so the database runs the attacker's SQL as well as the developer's. It happens when an application builds queries by joining user input into SQL text.
OWASP puts the core problem in one sentence: a SQL injection attack occurs when untrusted data becomes part of a query's SQL syntax, modifying the logic of the request. The full guidance is in the OWASP SQL Injection Prevention Cheat Sheet.
The malicious data usually arrives through an HTTP request, but it can also come from an email, a third-party API, an LDAP directory or even data already stored in the database.
MITRE catalogs the flaw as CWE-89, improper neutralization of special elements used in an SQL command. The attacker does not need database credentials. They only need an input that ends up inside a query — a login form, a search box, a URL parameter or a form field.
It is worth naming clearly: this is a vulnerability in the application, not in SQL itself. The database is doing exactly what it was asked to do. The problem is that the application asked it to run attacker-controlled text as code.
How does SQL injection work?
SQL injection works when an application builds a query as a single string and joins untrusted values into it. A quote character in the input closes the string the developer opened, and everything after it is parsed as SQL.
Take a simple login check. The application wants to find a user by name and password, so it builds a query like this:
SELECT * FROM users WHERE username = 'tom' AND password = 'secret'
Now suppose the attacker types tom' or '1'='1 into the username field. The query becomes a check where '1'='1' is always true, so the login can pass without the right password. The first quote closed the string the developer opened; the rest became SQL logic.
The fix is a parameterized query, also called a prepared statement. It sends the SQL structure and the data separately. The database fixes the shape of the query first, then treats the bound value as a single piece of data, whatever characters it contains. With parameters, the input tom' or '1'='1 is treated as the literal username to match, not as a condition added to the query.
One thing parameters do not do: they do not decide whether the caller may see the matching record. Authorization is a separate job. A parameterized query stops the attacker rewriting the query; your access checks still decide who sees what.
The same flaw affects object-relational mapping frameworks such as JPA and Hibernate, which have their own query languages. If you build those queries by concatenation, the same injection risk applies.
What are the main types of SQL injection?
The OWASP Web Security Testing Guide groups SQL injection into three classes by how the attacker gets results back: in-band, inferential (blind) and out-of-band. The underlying flaw is the same in each case.
| Type | How data comes back | What the attacker sees | Typical signal |
|---|---|---|---|
| In-band | Through the same channel | Data shown on the page or in an error message | Extra rows or database errors in the response |
| Blind (inferential) | Indirectly, by asking true-or-false questions | Changes in page behavior, timing or content | Page loads differently for true versus false conditions |
| Out-of-band | Through a separate channel the attacker controls | Data sent to an attacker server, often via DNS or HTTP | Outbound requests from the database server |
In-band SQL injection
In-band injection is the most direct form. The attacker uses the same channel to launch the attack and to see the results, for example by making the application return extra rows or by triggering a database error message that leaks information about the schema.
Error-based injection is a common subtype. A carefully crafted input causes a database error, and the error text reveals table or column names the attacker can use in the next attempt. This is why detailed database errors should never reach users.
Blind SQL injection
Blind injection applies when the application hides errors and results. The attacker still gets answers, just one bit at a time: they send a condition that is true or false, and watch whether the page loads normally, returns a different result, or takes longer to respond.
Time-based blind injection uses database functions that pause execution, such as a sleep command. If the page takes noticeably longer, the condition was true. It is slow, but automated tools can extract a whole database this way.
Out-of-band SQL injection
Out-of-band injection needs a database feature that can make an outbound request, such as a DNS lookup or an HTTP call. The attacker embeds a request that carries stolen data to a server they control. It is less common, because it depends on the database server being able to reach the internet, but it works even when the application shows nothing at all.
Second-order SQL injection
Second-order injection is worth knowing because it defeats input checks at the front door. The attacker stores a malicious value — in a username, a comment or a profile field — and it is only executed later, when another part of the application uses that stored value in a query. Data from your own database is still untrusted data.
What can an attacker do with SQL injection?
A successful SQL injection can read data, change data, delete data, run database administration operations and, in some cases, run operating system commands. The exact damage depends on the database features and the privileges of the account the application uses.
OWASP lists the possible impact plainly: read data from the database, modify database data, execute administration operations such as shutting down the database, recover the content of a file on the database server's file system or write files into it, and in some cases execute operating system commands.
In practice, attackers usually aim for one of these outcomes:
- Steal data — customer records, passwords, payment details, health records or trade secrets, dumped row by row.
- Bypass logins — log in as another user, often an administrator, without knowing their password.
- Change data — alter prices, balances, roles or permissions, or void transactions.
- Delete or destroy data — drop tables or wipe records, sometimes as a distraction or as ransom pressure.
- Take over the server — use database features to write files or run commands, turning a web flaw into full server control.
The blast radius is set by the database account. If the application connects as a highly privileged user, a single injection can do far more damage than if it connects with only the permissions it needs. That is why least privilege matters as much as the code fix.
Once an attacker has a foothold, they usually try to gain more access. Learn the signs in our guide to how to identify privilege escalation in SaaS apps.
Why does SQL injection still matter in 2026?
SQL injection is one of the oldest documented web flaws — it was first described in 1998 — and it still ranks near the top of the most dangerous weaknesses. The fix has been known for decades, yet vulnerable code keeps shipping.
In the 2025 CWE Top 25 list of the most dangerous software weaknesses, published by MITRE, SQL injection rose one place to second, behind cross-site scripting. In the OWASP Top 10:2025, the broader Injection category fell from third to fifth place, but that reflects a reordering of categories, not a solved problem. You can see the full ranking history in the SQL injection overview on Wikipedia.
In March 2024, CISA and the FBI published a joint Secure by Design alert on eliminating SQL injection vulnerabilities in software. They noted that MySQL introduced prepared statements, which separate commands from untrustworthy data, back in 2004 — two decades before the alert. The alert's message was blunt: software makers should stop shipping products with this flaw.
The lesson for small teams is simple. You do not need a large security budget to avoid this flaw. You need parameterized queries everywhere, a low-privilege database account, and a test that tries to break your inputs before an attacker does.
How do you prevent SQL injection?
Parameterized queries are the primary defense. Everything else is a supporting layer. OWASP's prevention cheat sheet is clear: define the SQL structure separately from its data values, then bind those values through the database API.
Use parameterized queries everywhere
Use parameterized queries for every dynamic data value in every query. That includes login forms, search boxes, filters, sort parameters and API endpoints. Concatenating untrusted values into the query before preparing it defeats the protection, so check that no code path builds SQL by joining strings.
For structural parts that cannot be parameterized — table names, column names, sort direction — map user choices to a strict allow-list of fixed, application-owned values. If the user picks something not on the list, reject it.
Give the database account the least privilege
Create a dedicated database account for each application, with only the permissions that application needs. A reporting app that only reads data should not be able to delete rows or run administration commands. Short-lived, narrowly scoped credentials reduce the damage further.
This matters because the credential your application holds decides how much damage a missed flaw can do. Least privilege is the ceiling on a successful injection.
Validate input — as a backup, not the fix
Allow-list validation (accept only known-good values, such as numbers where a number is expected) reduces the odds of an attack. Relying on manually escaping dynamic inputs is error-prone and is strongly discouraged by OWASP. Escaping rules differ between databases, and one missed case reopens the flaw.
Hide database errors and log suspicious activity
Show users a generic error page, never raw database errors. Error messages that leak table names, column names or query fragments give an attacker a map of your schema. Log failed queries and unusual input patterns so you can spot probing early.
A web application firewall can block many known injection patterns as an extra layer, but it is a filter, not a fix. Use it alongside fixed code, not instead of it.
How do you test for SQL injection?
Test your own inputs before an attacker does, and only test systems you own or have permission to test. Start with the inputs that reach the database: login forms, search boxes, filters, URL parameters and API fields.
- Try a single quote. Enter
'in a field and see whether the application errors or behaves differently. An error is a strong hint that input reaches SQL unprotected. - Try a classic condition. In a test environment, try an input such as
' OR '1'='1and see whether it changes the result set. - Check for blind signals. Try true and false conditions and compare the responses, including response times.
- Run a scanner. Use a web vulnerability scanner or a dedicated SQL injection tool against a staging copy, and review every finding by hand — scanners report false positives.
- Review the code. Search the codebase for string concatenation into SQL, dynamic query building and raw query methods. Every hit is a candidate flaw.
Fix what you find by parameterizing the query, then retest the same input to confirm it is now treated as harmless data. Keep the test cases in your regression suite so a later change cannot quietly reintroduce the flaw.
If you find a flaw in a vendor product or a third-party service, report it through the vendor's security contact or bug bounty program instead of testing it further.
What mistakes keep SQL injection alive?
The same mistakes appear in incident after incident. Avoiding them is most of the job.
- Escaping instead of parameterizing. Escaping feels like a fix, but it is fragile and database-specific. Parameters are the fix.
- Fixing only the login form. Attackers probe every input, including search, filters, sort orders and APIs. Parameterize all of them.
- Trusting stored data. Data from your own database, a partner API or a file can carry an injection payload. Treat all of it as untrusted.
- Using an admin database account. The application account should never be a database administrator. Least privilege limits the damage.
- Showing database errors to users. Detailed errors help attackers map your schema. Log them privately and show a generic message.
- Skipping tests after a fix. A fix that is never retested can regress silently. Keep injection tests in your suite.
The bottom line
SQL injection is a solved problem with an unsolved habit: building queries by joining strings. Parameterized queries, an allow-list for query structure and a least-privilege database account remove the flaw and cap the damage if one slips through.
Start with the code that touches your most sensitive data — logins, payments and customer records — then work outward. Check the headers on your own site with the free security headers checker.
For the wider controls that belong alongside this fix, see our small business cybersecurity checklist. It covers backups, updates and access control in one place.
Frequently asked questions
What is SQL injection in simple terms?
SQL injection is a trick where an attacker types database commands into a normal input box, such as a login or search field, and the application passes those commands to the database as if they were its own. The database then runs them, which can expose or change data the attacker should never see.
How does SQL injection work?
It works when an application builds a database query by pasting user input into the SQL text. A quote character in the input can close off the intended value and let the rest of the input be read as SQL commands. Parameterized queries stop this by sending the query structure and the data separately.
What is an example of SQL injection?
A classic example is entering tom' or '1'='1 in a username field. If the application joins that input into the query, the condition becomes always true, and the login check can pass without the right password. This only works against applications that build queries by string concatenation.
What is blind SQL injection?
Blind SQL injection is an attack where the application does not show database errors or results directly, so the attacker infers information by asking true-or-false questions and watching how the page responds, for example whether it loads normally or shows a different result.
Can SQL injection happen in stored procedures?
Yes, if a stored procedure builds dynamic SQL by concatenating its arguments into query text. Binding the procedure arguments protects the call itself, but it does not protect dynamic SQL constructed inside the procedure, which must also use parameters.
Is SQL injection still a problem in 2026?
Yes. SQL injection ranked second on MITRE's 2025 CWE Top 25 list of the most dangerous software weaknesses, and CISA and the FBI issued a joint Secure by Design alert in March 2024 urging software makers to eliminate it. The fix has existed for decades, but vulnerable code is still common.
Does input validation stop SQL injection?
Input validation helps, but it is not a reliable defense on its own. Attackers can often find inputs that pass validation yet still change the query. OWASP recommends parameterized queries as the primary defense, with allow-list validation as a backup, not a replacement.
Can a web application firewall stop SQL injection?
A web application firewall can block many known injection patterns, but it is a filter, not a fix. Determined attackers can often find payloads that evade it. Use it as an extra layer, and fix the code with parameterized queries so the vulnerability itself is gone.
Sources
The engineers and analysts who build Cysoro's domain scanner, email-authentication checks and free security tools. We test what we write about every day — SPF, DKIM, DMARC, certificates, look-alike domains and phishing — and check each guide against the relevant standards, government guidance and vendor documentation.
Checked against the OWASP SQL Injection Prevention Cheat Sheet, the CWE Top 25 2025 ranking and the CISA/FBI Secure by Design alert on SQL injection.
How we write and fact-check →