Skip to content
Securitybeginner

What is SQL injection?

Updated Nov 30, 20264 min readBased on official documentation
What is SQL injection?

In short

SQL injection is a security flaw where an attacker sneaks database commands into input that an application sends to its database. It happens when the program builds a query by joining user text straight into the SQL. The attacker can then read, change or delete data, and sometimes more.

What the attack looks like

Many websites store their data in a SQL database. When a user fills in a form, the application often places the typed text into a SQL query. SQL injection happens when the text contains SQL commands that change the meaning of that query.

SQL is the language used to ask a database for data. A database does not naturally separate the part of a query written by the developer from the part typed by a user. Both are just text. If the application glues them together carelessly, the database may run commands the developer never meant to allow.

A simple case shows the idea. Suppose a login form builds a query by pasting a username into a sentence. If the user types a quote character followed by extra SQL, the query changes shape. The database then answers a different question than the one the developer wrote.

Why it matters

The official OWASP description lists several consequences. Because SQL databases usually hold sensitive information, an attacker may read data they should never see. The attacker may also change or delete records. In some cases, a weak login check can let someone sign in as another user without knowing that user's password.

Authorization can be affected too. If the database stores who may do what, an attacker who changes those rows can grant themselves more access. The same documentation notes that the severity depends on the attacker's skill, and that defences such as limited database accounts can reduce the damage. It still treats SQL injection as a high impact problem.

The flaw is also easy to find. Any site that talks to a database through user input can be a target, including small sites with few visitors. That is why it appears so often in security checklists.

How it works

The cause is a query built by joining strings. Imagine code that takes a name from a form and builds this text: SELECT * FROM items WHERE owner = 'alice'. The quotes wrap the value so the database knows where it begins and ends.

If the user types a quote character inside the value, the quote closes the string early. Everything after it becomes SQL that the database reads as commands. An attacker can add a condition that is always true, so the query returns every row instead of only the user's own rows. The documentation gives this kind of example for an items table.

Some database servers also accept several statements separated by semicolons in one request. In those systems, an attacker may be able to add a second statement that deletes data. Other servers reject this, but the core flaw is the same: user input is treated as part of the command.

How to prevent it

The main fix is to use parameterized queries, sometimes called prepared statements. In this style, the query has fixed SQL with placeholders. The user's text is sent separately as data, so the database never reads it as commands. The OWASP guidance says this approach needs less maintenance and gives stronger guarantees than trying to clean input by hand.

Here is a small pattern in Java, using a placeholder for each value:

String query = "SELECT id, firstname, lastname FROM authors WHERE firstname = ? AND lastname = ?"; PreparedStatement pstmt = connection.prepareStatement(query); pstmt.setString(1, firstname); pstmt.setString(2, lastname);

The same idea exists in most languages and database libraries. Always look for the parameter feature in your library before you write a query by hand.

Common mistakes

A common mistake is to rely on a blocklist. The developer tries to remove or escape dangerous characters. The documentation warns that this approach is full of loopholes. Attackers can target fields that are not quoted, find ways around escaped characters, or hide commands inside stored procedures.

Another mistake is to assume that stored procedures solve the problem. They can block some attacks, but a procedure that builds SQL from its inputs is still vulnerable. Use parameters inside the procedure as well.

Finally, do not forget input validation. An allowlist of expected characters, such as digits for an ID, adds a useful layer. Our guide to input validation explains how to check data before it reaches your code. Validation alone is not enough, though, so keep parameterized queries as the main defence.

You can also limit damage with a database account that has only the rights the application needs. If the app only reads a table, it should not be able to delete rows. This is defence in depth: it does not stop the injection, but it limits what an attacker can do.

Testing helps too. The OWASP Testing Guide describes how to look for SQL injection in a running application, and code review guides show what to search for in source code. Use these checks on your own applications, and only with permission on systems you are allowed to test.

For a wider view of how attackers exploit weak inputs and how to protect your supply chain, read our page on software supply chain attacks. Knowing the common threats makes it easier to spot SQL injection risks early.

FAQ

Is SQL injection still a problem today?

Yes. The OWASP documentation describes it as very common in database-driven websites, because the flaw is easy to find and exploit. Modern frameworks help, but code that builds queries by string joining is still risky.

Does using an ORM protect me automatically?

Usually an ORM uses parameters for you, which reduces the risk. However, if you write raw SQL inside it or build query fragments from user input, the protection can be lost. Check how your tool handles raw queries.

Can a firewall stop SQL injection?

A web application firewall can block some attack strings, but attackers often find ways around such filters. The documentation treats it as an extra layer, not a fix. The real defence is parameterized queries in your code.

What is the first thing I should do to protect my app?

Replace every string-built SQL query with a parameterized query. Then give the database account only the permissions it needs. These two steps prevent most common attacks.

Sources

This page is based on the official documentation below. Read it for full detail.

SQL injection — official documentation ↗
blozhikDeveloper news from across the web, explained. Every story links to its original sources.