SQL Injection Attacks Explained: From Basics to Advanced
Back to Blog
Web Security5 min read18,879 views

SQL Injection Attacks Explained: From Basics to Advanced

Why SQL injection happens from first principles, the three families it comes in, what it really lets an attacker do, and the one fix that actually ends it — parameterised queries.

Z

ZeroDaysHack Team

November 18, 2026

SQL injection is one of the oldest web vulnerabilities and still one of the most damaging — it shows up on the OWASP Top 10 decade after decade because the root cause is so easy to introduce and so easy to miss. This guide explains why it happens from first principles, the three families it comes in, and — most importantly — the one fix that actually ends it. Understand the cause and both attacking it (in your lab) and defending against it stop being magic.

#The root cause: data treated as code

Strip away the jargon and SQL injection is one mistake: user input gets glued directly into a database query, so the database can't tell your data apart from its own instructions.

How SQL injection happens when input is concatenated into a query
The whole bug: when input is concatenated into the query string, the database runs it as SQL instead of reading it as data.

Picture a login form that builds its query by sticking the username straight into a string. The developer intended the input to be a value. But because it's concatenated into the SQL text, a crafted input can close the quote and add logic of its own — and the database, seeing valid syntax, obediently runs it. That's the entire vulnerability.

The classic teaching example is the tautology: an input that makes the WHERE clause always true, so a login check that should match one user matches everyone. You'll see it in every SQLi lesson because it demonstrates the core idea in one line — the input stopped being data and became logic.

#The three families of SQL injection

All SQL injection shares that root cause. What differs is how the answer comes back to the attacker.

The three families of SQL injection
Same root cause, three feedback channels: in-band, blind, and out-of-band.
FamilyHow you see resultsNotes
In-bandDirectly in the pageUnion-based pulls data into the output; error-based leaks it through DB error messages. Fastest to exploit.
BlindYou infer it indirectlyBoolean — the page changes on true vs false; time-based — the database pauses when a guess is right. Slow, one bit at a time, but just as serious.
Out-of-bandThrough a different channelThe database is coaxed into making a DNS or HTTP request back to the attacker. Used when results and errors are both suppressed.

#What an attacker can actually do

Once input reaches the database as code, the impact scales with what that database can do and how locked-down the app's DB account is:

  • Read data that isn't theirs — other users' records, password hashes, tokens.
  • Bypass authentication — the always-true login trick.
  • Modify or delete data — if the account has write access.
  • Pivot deeper — some databases expose file reads or command execution, turning a web bug into a foothold on the server.

This is why SQLi is rated so highly: a single injectable parameter can expose an entire database.

#The fix that actually works: parameterised queries

Here's the good news — SQL injection is one of the most completely solvable vulnerabilities. The fix is parameterised queries (also called prepared statements with bound parameters). You send the query structure and the data separately, so user input can never be parsed as SQL.

The vulnerable pattern — input concatenated into the query (don't do this):

vulnerable: input becomes part of the SQL text
# BAD — the username is glued into the query string
query = "SELECT * FROM users WHERE name = '" + username + "'"
cursor.execute(query)

The fix — the input is sent as a bound parameter, never as text:

safe: the driver sends structure and data separately
# GOOD — '?' is a placeholder; the value is bound, not concatenated
cursor.execute("SELECT * FROM users WHERE name = ?", (username,))

In the safe version the database is told, in effect, "here is the query, and separately, here is a value to slot into that one hole." No input — however crafted — can escape the placeholder and become logic. The bug is gone, not merely filtered.

#Defence in depth

Parameterised queries are the fix. The remaining layers reduce damage when something slips through — a legacy query, a new hire's shortcut, a third-party component.

Layered defences against SQL injection
Parameterise first; the other layers shrink the blast radius when something slips through.
  1. Parameterised queries — the real fix. Data can never become code.
  2. Least privilege — the app's database user can't drop tables or read other schemas, so a bug exposes less.
  3. Input validation — allow-list expected formats. Helpful, but never a substitute for layer 1.
  4. WAF and monitoring — catches known patterns and flags probing. Buys time; bypassable on its own.
  5. Generic error handling — don't return raw database errors to users; that's what feeds error-based SQLi.

#Practise it properly

Reading about SQLi gets you maybe a third of the way. To really get it, find one by hand in a safe target and then fix it.

#Next steps

You now understand SQL injection the way that actually transfers: a root cause (data treated as code), three feedback channels, and a definitive fix. The companion read on this blog is XSS, the other classic injection bug — same lesson (never mix untrusted input into something an interpreter runs), different interpreter. And if you want to understand the layer underneath all of it, start with Networking for Hacking.