SQL Injection Explained: How Attackers Hijack Databases

SQL injection explained in plain terms means an attack where a criminal types database commands into an ordinary form, such as a login box, and the website runs them by mistake. The central question is how a few characters typed by a stranger can take over a database, and the short answer is that the website mixes the attacker’s input into its own database query, so the database cannot tell the user’s words from the developer’s instructions.

Think of a bank teller who follows written instructions exactly. If you hand over a form that says transfer money to this account, and the teller cannot tell your handwriting from the bank’s printed instructions, the transfer happens. SQL injection works the same way: the database follows instructions, and the attacker has smuggled their own into the pile.

It is one of the oldest and most damaging web attacks, and it still appears in breach reports year after year. The surprising part is not that it exists, but that it is entirely preventable, and has been for decades.

Key takeaways

  • SQL injection happens when user input is mixed into a database query without proper separation.
  • Attackers can read, change, or delete data, and sometimes take over the whole server.
  • Login forms, search boxes, and web addresses are all possible entry points.
  • The main defence is parameterised queries, which keep data and instructions apart.
  • It is one of the most common and most preventable web vulnerabilities.

What SQL injection is

SQL injection is a vulnerability in which an attacker inserts SQL, the language databases use, into a query through input the application does not properly handle. SQL is the language of databases: it asks for rows, inserts records, updates values, and deletes tables. When an application builds a query by pasting user input directly into the SQL text, an attacker can add their own SQL, and the database will run it as if the developer wrote it.

A typical target is a login form. The application might build a query that looks for a user with the name and password just typed. An attacker who types a carefully crafted value can change the meaning of that query, for example by adding a condition that is always true, so the database returns a user record without any valid password. The attacker is logged in as someone else, and the application never suspects a thing.

The same trick works in search boxes, web addresses, and any place where input reaches the database. An attacker can ask the database to reveal its contents table by table, to change prices, to delete records, or to hand over password hashes. In the worst cases, database commands can be used to write files or run commands on the server itself, turning a database flaw into a full takeover.

SQL injection has been known since the late 1990s, and it remains near the top of the most common web vulnerabilities. Our guide to database indexing explains how databases organise and retrieve their information, and why a query that reaches the wrong rows is so dangerous.

How SQL injection works

It begins with an application that builds queries by joining strings together. A developer writes a query template with a gap for the username, then fills the gap with whatever the user typed. If the user types a normal name, the query works as intended. If the user types a name containing SQL punctuation, the query changes shape, and the database runs the attacker’s version instead.

The classic example uses a single quote, which in SQL marks the end of a text value. By typing a quote, then a condition such as or 1=1, then a comment marker that hides the rest of the query, the attacker turns a login check into a query that always succeeds. The comment marker is a small but telling detail: the attacker does not need to know the rest of the query, only how to silence it.

Once the door is open, attackers usually explore. They ask the database what tables it holds, what columns those tables contain, and then they extract the interesting ones: usernames, emails, password hashes, payment details. They do this slowly, one question at a time, using the application’s own responses as their guide, a process that can be automated to steal an entire database in hours.

More advanced attacks go further. Some databases let SQL commands read files from the server, or write new ones. Others allow commands that run operating system programs. An attacker who reaches that point no longer needs the website at all: they have a foothold on the server, and from there they can move deeper into the network.

A photograph of hands typing on a backlit keyboard in a dark room, a laptop screen glowing green behind them
SQL injection turns an ordinary login box into a doorway: the database runs the attacker’s commands because it cannot tell them from the developer’s own query.

Comparing SQL injection with other web attacks

Aspect SQL injection Other web attacks
What it targets The database behind the app Users, sessions, or the server
Entry point Any input reaching a query Forms, links, or uploads
Typical damage Stolen or destroyed data Stolen sessions or defacement
Root cause Input mixed into SQL text Missing checks or poor design
Main defence Parameterised queries Varies by attack

Where SQL injection happens

Login forms are the best-known target, because a successful attack there hands over an account directly. But any input that reaches the database is a candidate: search boxes, product filters, comment forms, and values carried in web addresses. An attacker probes each one, watching for error messages or odd behaviour that reveal a query is being built from their input.

Old and neglected applications are the most vulnerable. A system written years ago, before secure coding practices were standard, may build queries by joining strings throughout its code. Such systems often run quietly for years, and nobody notices the flaw until a breach makes it obvious. Our guide to network security explains how unusual database activity can be spotted, even when the vulnerable code itself goes unnoticed.

Modern frameworks have reduced the risk, because they encourage parameterised queries by default. But the flaw survives wherever developers write raw SQL by hand, in custom code, in reports, or in administrative tools. It also appears in APIs and in places where input arrives from another system rather than from a person, so the entry point is not always a form a user can see.

Third-party software is a common source too. A plugin, a content management extension, or a bought-in component may contain the flaw, and the organisation using it may have no idea. Keeping software updated matters, because fixes for injection flaws are among the most common security patches released.

Strengths and limits

From the attacker’s side, the strength of SQL injection is its power. A single flaw can expose an entire database, and the attack can be carried out with nothing more than a web browser and patience. Automated tools make it worse: they can scan thousands of sites, find the vulnerable ones, and extract their data with little human effort.

From the defender’s side, the strength is that the fix is well understood and complete. Parameterised queries, in which the database receives the query and the data separately, remove the flaw entirely, because the database never treats data as instructions. This is not a partial mitigation; it is a cure, and it has been the standard advice for many years.

The limits of the attack are that it needs a vulnerable application in the first place. A system built with parameterised queries throughout, with least-privilege database accounts, and with input validation, gives an attacker nothing to work with. The attack also leaves traces: unusual queries and error messages that monitoring can catch.

The limits of the defence are human. The fix must be applied everywhere, not just in the obvious places, and old code must be reviewed. A single forgotten query, in an admin page nobody visits, can be enough, which is why testing and code review matter as much as the fix itself.

SQL injection checklist

  • Use parameterised queries everywhere user input reaches the database.
  • Never build SQL by joining strings that contain user input.
  • Validate input, but do not rely on validation alone as the defence.
  • Give the application’s database account the least privilege it needs.
  • Do not show detailed database errors to users.
  • Keep frameworks, plugins, and libraries updated.
  • Test for injection with automated scanning and manual review.
  • Monitor database queries for unusual patterns and errors.
  • Encrypt sensitive data so a stolen database is less useful.

Frequently asked questions

What is SQL injection?

SQL injection is an attack in which a criminal inserts database commands into input, such as a login form, and the application runs them because it mixed the input into its own query. The attacker can then read, change, or delete data, and sometimes take over the server.

How does SQL injection steal data?

By changing the meaning of a query so the database returns more than it should. An attacker can ask for the list of tables, then the contents of the interesting ones, extracting usernames, emails, and other records one query at a time, often with automated tools.

What is a parameterised query?

It is a query in which the database receives the SQL and the data separately. The structure of the query is fixed in advance, and user input is treated only as a value, never as instructions, so injection becomes impossible.

Can input validation stop SQL injection?

It helps, but it is not enough on its own. Attackers are creative about encoding and bypassing filters, and validation rules can be missed or wrong. Parameterised queries are the reliable defence; validation is an extra layer.

Is SQL injection still a problem today?

Yes. It remains one of the most common web vulnerabilities, appearing in breach reports every year. It survives in old code, in custom-built systems, and in third-party components, even though the fix is well known.

What should I do if my site is vulnerable?

Fix the code to use parameterised queries, review every place input reaches the database, and check logs for signs of exploitation. If data may have been stolen, treat it as a breach: inform those affected and rotate credentials.

The bottom line

SQL injection explained in one line is this: a database that cannot tell a user’s words from its own instructions will run whatever it is given.

The fix is simple, well understood, and complete: keep data and instructions apart with parameterised queries, and give the database account the least privilege it needs. An attack this old should have no new victims.

This article is general information about how SQL injection works, not advice about securing a particular application.

Latest articles

Scroll to Top