SQL injection
When user input becomes code, your database does the attacker's bidding.
SQL injection happens when unsanitized input is concatenated straight into a query, so a payload like ' OR '1'='1 changes the query's meaning — dumping data, bypassing auth, even dropping tables. The fix is to stop mixing code and data: parameterized/prepared statements bind input as values, never as SQL.
Normal query
A benign username like 'alice' flows from the form through the app into a query built by string concatenation. The resulting SQL means exactly what the developer intended, and the database returns Alice's row. Everything looks fine because well-behaved input never reveals the flaw.
Injected payload
Instead of a username the attacker submits ' OR '1'='1. Because the app glues input directly into the query text, the closing quote ends the intended value and the OR clause is parsed as live SQL. The WHERE condition is rewritten to be always true, so the query's very meaning changes.
Database compromised
The database has no way to know this SQL was half-written by an attacker — it faithfully executes the altered query. That can dump every row in the table, log the attacker in as the first user, or with stacked queries even drop tables. The data was parsed as code, and the database did the attacker's bidding.
Parameterized query blocks it
With a parameterized (prepared) statement, the SQL structure is sent to the database first with placeholders, and the input is bound separately as a pure value. The payload ' OR '1'='1 is now stored as a literal string to search for, never parsed as SQL. Code and data stay separated, so the attack is defused at its root.
The problem: mixing code and data
Almost every application builds SQL queries that include values from users — a username to log in, a search term, an id from the URL. The trouble begins the moment the developer builds that query by gluing strings together:
"SELECT * FROM users WHERE name = '" + input + "'"
To the developer, input is *data* — a value to search for. But the database doesn't receive "a query plus some data"; it receives one finished string of SQL text that it must parse from scratch. If the user's input contains SQL syntax, that syntax becomes part of the program the database runs. The boundary between "instructions" and "the values those instructions operate on" has quietly dissolved.
This is the entire essence of SQL injection: untrusted input is interpreted as *code* rather than treated as *data*. It's a specific case of a much broader class of injection flaws (command injection, LDAP injection, and so on), all sharing the same root cause — an interpreter can't tell which parts of a string the developer trusted and which parts an attacker supplied. SQL injection has topped security risk lists for over two decades precisely because the vulnerable pattern is so natural to write and so easy to overlook when your own testing only ever uses well-behaved input.
How the attack actually works
Take a classic login check built by concatenation:
SELECT * FROM users WHERE user = '$u' AND pass = '$p'
With a normal username the quotes wrap the value harmlessly. Now the attacker types ' OR '1'='1 into the username field. The query text becomes:
SELECT * FROM users WHERE user = '' OR '1'='1' AND pass = '...'
Read it as the database does: the attacker's leading quote closes the empty username string, and OR '1'='1' injects a condition that is *always true*. The WHERE clause now matches every row, so the query returns a user (typically the first, often an admin) and the login is bypassed without knowing any password. The single quote is the pivot — it lets the attacker escape out of the data context and into the SQL grammar.
From that foothold the damage scales. Injecting UNION SELECT lets an attacker append data from *other* tables onto the results — reading password hashes, tokens, or customer records. Comment markers like -- truncate the rest of the intended query. On databases that allow stacked queries, a payload ending ; DROP TABLE users; -- can destroy data outright. The database is not malfunctioning — it is doing exactly what the text told it to. That's what makes injection so dangerous: the attacker is writing part of your program.
The classes of injection
Injections are usually grouped by *how the attacker gets the answer back*, which matters because it dictates how they probe.
In-band (classic) injection is the easiest: the results come back through the same channel the attacker used. Two sub-types dominate. In error-based injection, the attacker deliberately provokes database error messages that leak schema or data — a reason detailed SQL errors should never reach users. In union-based injection, the attacker uses UNION SELECT to graft data from other tables directly onto the visible query results.
Blind injection happens when the app never shows query results or errors — but is still injectable. The attacker infers data one bit at a time by asking true/false questions. In boolean-based blind injection, they watch whether the page renders differently for a true versus false condition (e.g. "if the first letter of the admin password is 'a', return a valid page"). In time-based blind injection, there's no visible difference at all, so they inject something like IF(condition, SLEEP(5), 0) and *measure how long the response takes* — a five-second delay means "true". It's painstaking but fully automatable, which is why tools like sqlmap can extract an entire database blind.
Out-of-band injection is the rarest: the attacker makes the database itself send data to an external server they control (via a DNS or HTTP request), used when the direct channels are unavailable. Understanding these classes matters because a site that "shows no errors and no data" is emphatically *not* safe — blind techniques defeat that assumption.
The impact — and why it's severe
SQL injection is consistently ranked among the most dangerous web vulnerabilities because a single flaw can compromise the confidentiality, integrity, and availability of an application's entire data layer at once.
- Confidentiality — the attacker can read data they should never see: every user's credentials, personal information, payment details, or business secrets. Many of the largest historical breaches began with a single injectable parameter.
- Authentication bypass — as the login example showed, injection can log an attacker in as any user, including an administrator, without a password.
- Integrity — beyond reading, an attacker can
UPDATEorINSERTdata: change prices, grant themselves admin rights, alter records, or plant malicious content. - Availability —
DELETEorDROPstatements can wipe tables and take the application down. - Escalation beyond the database — depending on configuration and database features, injection can sometimes read server files, write files, or even execute operating-system commands, turning a data breach into a full server compromise.
The severity is amplified by how *reachable* the flaw is: it's exploitable remotely, often unauthenticated, through an ordinary form field or URL parameter, and it can be found and exploited by automated tools at internet scale. One overlooked concatenated query in one endpoint is enough.
The real fix: parameterized queries and defense in depth
The one true fix addresses the root cause — stop mixing code and data. Parameterized queries (also called prepared statements) send the SQL structure to the database *with placeholders*, then send the user's input *separately* as bound values:
SELECT * FROM users WHERE user = ? AND pass = ?
The database parses and compiles the query before it ever sees the input, so the placeholders' values can *only* be data — they can never change the query's structure, no matter what characters they contain. Feed ' OR '1'='1 into a bound parameter and the database dutifully searches for a user literally named ' OR '1'='1, finds none, and the attack collapses. This is why parameterization defeats injection *by construction*, not by trying to guess-and-filter malicious strings. Stored procedures achieve the same effect when they use bound parameters internally, and ORMs (like Hibernate, ActiveRecord, or Prisma) parameterize for you — which is a large part of why they're recommended — provided you don't drop back to raw string-concatenated SQL.
Two anti-patterns to reject: blocklisting/escaping "bad characters" is notoriously leaky (encodings, edge cases, and forgotten paths defeat it), and it should never be the primary defense. Parameterization is.
Because no single control is perfect, layer defense in depth on top: validate and constrain input by type and format (an id should be an integer); apply least privilege so the app's database account can't drop tables or read other schemas, limiting the blast radius if something slips through; never expose raw database errors to users; and deploy a Web Application Firewall (WAF) to filter obvious injection attempts. Crucially, a WAF is a *supplement*, not the fix — it catches known patterns but can be bypassed. Get the queries parameterized first; everything else is there to contain the damage if a mistake ever gets through.
An attacker injects malicious SQL through unsanitized input, e.g. ' OR '1'='1 to make a WHERE clause always true.
Consequences range from dumping data and bypassing authentication to dropping entire tables.
The root cause is string-concatenating input directly into the query so data is parsed as code.
Parameterized/prepared statements bind input as values, keeping it out of the SQL grammar.
Defense in depth adds input validation, least-privilege DB accounts, ORMs, and a WAF.
Quick quiz
Why does ' OR '1'='1 work as an injection payload?
What is the primary defense against SQL injection?
How a SQL query executes
The pipeline that turns a text string into rows fetched off disk.
Database index types
The data structures that turn a full-table scan into a pinpoint lookup.
Sessions, cookies, JWT & OAuth 2.0
How a server remembers who you are — and who let you in.