What is SQL injection?

DEFINITION

SQL injection is a web vulnerability where untrusted input is mixed into a database query, letting an attacker read, change or delete data they should not access.

Most websites and apps store data in a database queried with SQL. SQL injection happens when an application builds a query by pasting user input, such as a search term or login field, directly into the SQL text. If that input contains characters with special meaning in SQL, the database may treat part of it as a command instead of plain data, changing what the query does.

The result can range from exposing hidden records to bypassing logins, modifying prices, or deleting tables, depending on the query and the database permissions involved.

Why SQL injection matters

Injection has been a well-known category in the OWASP Top 10 for years because the impact can be severe, often a full data breach of customer records. Automated tools probe forms and URLs for these flaws at scale, so even a small site with a vulnerable search box or old plugin can be found and abused.

How to prevent SQL injection

The reliable fix is to keep code and data separate. Use parameterized queries (also called prepared statements) or a well-maintained ORM, so user input is always sent to the database as a value, never as part of the command. For example, a product search should pass the search term as a parameter to a fixed query rather than concatenating it into the query string. Add defense in depth: validate input against expected formats, give the application's database account only the permissions it needs, avoid showing raw database errors to visitors, and put a web application firewall in front of the site. Review legacy code and plugins, where older string-building patterns often survive.

Best practices

  • Use parameterized queries or an ORM for every database call, without exceptions.
  • Run the application with a database user that cannot drop tables or access unrelated data.
  • Show generic error messages to users and log technical details privately.
  • Include injection checks in code reviews, automated scans and penetration tests.

Common mistakes

  • Relying on blocklists that strip certain characters instead of using parameterized queries.
  • Assuming an ORM makes every query safe, even when raw SQL strings are used inside it.
  • Trusting values from hidden fields, cookies or headers because users cannot see them.

Frequently asked questions

Is SQL injection still common?

It is less common in modern frameworks that use parameterized queries by default, but it still appears in custom code, legacy systems and poorly maintained plugins. Injection remains a category in the OWASP Top 10, and automated scanners continually look for it, so it should still be tested for in every web application.

Can a web application firewall stop SQL injection?

A WAF can block many common injection patterns and is valuable as an extra layer, especially for older systems you cannot quickly fix. However, attackers sometimes find ways around WAF rules. The real fix is in the code: parameterized queries remove the vulnerability rather than filtering attempts to exploit it.

Does input validation prevent SQL injection?

Validation helps by rejecting data that does not match expected formats, such as letters in a numeric ID field. On its own it is not sufficient, because many legitimate inputs, like names with apostrophes, contain characters meaningful to SQL. Combine validation with parameterized queries for proper protection.

Need help with this?

사이버 보안 by VYZEON — 비즈니스, 데이터, 고객을 안전하게 지키세요.

Fixed packages from $199. Written scope, no surprises.

See 사이버 보안

Related terms

More Security & cloud terms