What Is SQL Injection? Examples, Types and How to Prevent It

SQL injection has been around for more than 25 years, and it still shows up in real applications every week. It is one of the first bugs every penetration tester learns, and one of the most damaging when it is found in production. In this guide we explain what SQL injection is, how it works with simple examples, the main types, and the right way to prevent it.

What is SQL injection?

SQL injection (SQLi) happens when an application builds a database query by joining user input directly into the SQL text. If the input contains SQL syntax, the database runs it as part of the query. An attacker can then read data they should not see, change or delete records, or log in without a password.

Injection is ranked A05 in the OWASP Top 10:2025, and SQL injection is its best known form. You can read about all ten categories in our guide OWASP Top 10 (2025) explained.

A simple SQL injection example

Imagine a login form. Behind it, the code builds a query like this:

query = "SELECT * FROM users WHERE username = '" + username + "' AND password = '" + password + "'"

A normal user types alice and their password, and the query works as expected. Now an attacker types this into the username box:

admin' --

The query becomes:

SELECT * FROM users WHERE username = 'admin' --' AND password = '...'

In SQL, -- starts a comment, so everything after it is ignored, including the password check. The database simply returns the admin account and the attacker is logged in. The application never meant for the user to write SQL, but it allowed it by mixing data and code.

What attackers can do with SQL injection

  • Bypass login without knowing any password.
  • Read sensitive data such as user details, password hashes, orders and payment records.
  • Change or delete data, for example editing prices or wiping tables.
  • Take over the server in some setups, when the database user has high privileges.

A single injectable parameter can expose the whole database, which is why SQL injection is usually rated high or critical with CVSS.

Types of SQL injection

  • In-band (classic) SQLi: the result of the injected query appears directly on the page, for example through error messages or a UNION SELECT that adds extra rows.
  • Error-based SQLi: detailed database errors reveal table names, columns or data.
  • Blind boolean-based SQLi: the page shows no data, but it behaves differently when a condition is true or false, so the attacker extracts data one question at a time.
  • Blind time-based SQLi: the attacker makes the database pause, for example with SLEEP(5), and measures the response time.
  • Out-of-band SQLi: the database is tricked into sending data to an external server, for example through a DNS lookup.
  • Second-order SQLi: malicious input is stored safely at first, then used unsafely later in a different query.

Where SQL injection hides

Login forms are only the start. During a test we check every place where input reaches a query:

  • Search boxes, filters and sorting options, such as ?sort=price
  • URL parameters like /product?id=42
  • JSON and XML bodies in APIs
  • Cookies and HTTP headers such as User-Agent or X-Forwarded-For
  • Report and export features that build complex queries
  • Desktop (thick client) apps that talk to a database directly

How security testers find SQL injection

This is the general approach, and only ever on applications we have written permission to test:

  1. Map every input that could reach the database.
  2. Send characters that break SQL syntax, such as a single quote ', and watch for errors or changes in the response.
  3. Test true and false conditions, for example AND 1=1 compared with AND 1=2, to spot blind injection.
  4. Use time delays to confirm blind injection when the page looks the same.
  5. Confirm the finding carefully with tools like Burp Suite and, where allowed, sqlmap.
  6. Report the impact without dumping real customer data.

Important: testing for SQL injection on systems you do not own or have permission to test is illegal in India under the IT Act, 2000, and in most countries. Practice on intentionally vulnerable labs such as DVWA or PortSwigger Web Security Academy.

How to prevent SQL injection

The fix is well known and reliable: never build SQL by joining strings with user input. Use parameterized queries (also called prepared statements), so the database always treats input as data, never as code.

Python example

cursor.execute("SELECT * FROM users WHERE username = %s AND password_hash = %s", (username, password_hash))

PHP (PDO) example

$stmt = $pdo->prepare("SELECT * FROM users WHERE email = ?");
$stmt->execute([$email]);

Node.js example

const [rows] = await db.execute('SELECT * FROM orders WHERE id = ?', [orderId]);

Along with parameterized queries:

  • Use an ORM safely. ORMs help, but raw query functions inside them can still be vulnerable.
  • Allow-list dynamic parts. Table names and sort columns cannot be parameterized, so only accept values from a fixed list.
  • Use least privilege. The application's database user should not be an admin and should only access what it needs.
  • Hide detailed errors. Show a generic message to users and log the details on the server.
  • Add a WAF as an extra layer, not as the main fix.
  • Test regularly, especially after new features that add search, filters or reports.

Quick SQL injection checklist for developers

  • Is every query that uses input built with parameters?
  • Are sort fields, column names and table names checked against an allow-list?
  • Are raw SQL calls inside the ORM reviewed?
  • Does the database user have only the permissions it needs?
  • Are database errors hidden from users?

Frequently asked questions

Is SQL injection still common?

Yes. Modern frameworks make safe queries easier, but SQL injection still appears in custom search features, reporting modules, older code and internal tools.

Does input validation alone stop SQL injection?

No. Validation helps, but attackers often find ways around filters. Parameterized queries are the real fix, with validation as an extra layer.

Can a web application firewall stop it?

A WAF can block many common payloads, but it can be bypassed. Treat it as a safety net while you fix the code.

Get your application tested

At Bugfynd we test web applications, APIs and thick client apps for SQL injection and the rest of the OWASP Top 10, with every finding explained in plain language and a free retest after you fix it. See what you receive in our sample pentest reports, or learn to find these bugs yourself in our web application pentesting course taught in English and Telugu.

Questions or a project to discuss? Email contact@bugfynd.com or message us on WhatsApp.