Please enable JavaScript to use CodeHS

Cybersecurity Basics

What Is SQL Injection?

A login box just asks for a username and password — it shouldn't be able to hand over an entire database. But if a site isn't careful, typing the right handful of characters into that box can trick it into doing exactly that. Here's how, and how developers close the hole.

Security ~10 min Beginner friendly
The Big Idea

What is SQL injection?

SQL (usually said "sequel" or spelled out "S-Q-L") is a language for asking a database questions — things like "give me every user whose username is X" or "find all orders placed today." Nearly every website that stores logins, comments, orders, or grades is quietly running SQL commands behind the scenes every time you click something. (If you want a refresher on how those ordinary queries work, see Data and Databases: SQL Queries.)

Websites usually build those commands by gluing fixed text together with whatever a user just typed — a username, a search term, a comment. SQL injection is what happens when a site does that gluing carelessly, so a user's typed input can slip out of being "just a value" and become part of the command itself.

Simple definition: SQL injection is a trick where an attacker types text into a normal input box — like a username field — that gets treated as part of the database command instead of just a piece of data. That can let them read information they shouldn't, or even log in without ever knowing a real password.

Think of it like a mail-merge letter that says "Dear <NAME>, your balance is $<AMOUNT>." Normally you just fill in a name. But if the letter-writer isn't careful about what counts as "just a name," and you write something sneaky enough in that blank, you could get it to print something the template was never designed to say. SQL injection is that same idea, aimed at a database command instead of a letter.


How It Works

Walking through a login form

Picture a simple login form with a username box and a password box. A careless version of the app might build its database command like this, just by pasting the typed text into the middle of a sentence:

SELECT * FROM users WHERE username = 'INPUT' AND password = 'INPUT';

In plain English, that command means: "Look in the users table, and give me back the row where the username matches this and the password matches that." If both match, the app logs you in as that user.

The safe-looking case

Typed into the username box: sam_the_student

honest-login.sql
SELECT * FROM users
WHERE username = 'sam_the_student'
  AND password = 'whatever123';

Output

1 row found.
Logging in as sam_the_student.

The malicious case

Now say an attacker types this into that exact same username box, with no other safeguards in place:

Typed into the username box: ' OR '1'='1' --

attacker-login.sql
SELECT * FROM users
WHERE username = '' OR '1'='1' -- '
  AND password = 'anything';

Output

3 rows found: jordan_lee, ms_alvarez, admin
Logging in as jordan_lee — no password required.

Here's exactly why that happens:

  1. The username box was supposed to hold one plain word. Instead, the very first character typed is a quote ( ' ), which is the same character the app uses to mark "the value starts here" and "the value ends here." That quote closes the value early — the database now thinks the username value was just an empty string, and everything after it is back to being command, not data.
  2. What comes next, OR '1'='1', is a brand-new condition tacked onto the command. '1'='1' is always true — the text "1" always equals the text "1" — so this condition is always true, for every single row in the table.
  3. Because it's joined with OR, the whole WHERE clause becomes true no matter what the username or password actually are. "Match this specific person" turns into "match everyone."
  4. The -- marks the rest of the line as a comment in SQL — it tells the database to ignore everything after it. That deletes the closing quote and the entire password check from the command, so it never even runs.

The result: a command that was supposed to check one specific username and password now returns every row in the table, and the app logs the attacker in — usually as whichever account it finds first.


Interactive

Try it yourself

Below is a simplified login form connected to a fake little database. Switch between the Vulnerable site (builds its command the careless way you just saw) and the Fixed site (uses a parameterized query, explained below). Type the same input into both and watch the assembled query — and the result — change.

Login simulator — schoolportal.example
Try:

To keep the focus on the query text, this demo checks only the username. A real login also checks the password — but the same trick works if an attacker aims it at that box instead.

Query the server runs


                
READY

Type a username and click Log In to see what happens.

On the vulnerable site, the malicious text gets pasted straight into the command, so the quote inside it breaks out and rewrites the logic. On the fixed site, the database is told up front exactly which part of the command is a value — so no matter what characters are inside it, quotes included, it's only ever compared as plain text, never run as part of the command. That's a parameterized query (also called a prepared statement): the real fix, covered in the defenses section below.


Why It Matters

Why SQL injection is such a big deal

SQL injection isn't some rare, theoretical bug. For years it has ranked among the most common serious vulnerabilities in real websites, showing up again and again on industry lists like the OWASP Top 10 — a widely used ranking of the web's most dangerous security weaknesses.

It's rarely just one account. A single successful injection can return an entire table at once — every username, password, and personal detail stored in it — not just the attacker's own login.

Any input box can be the door. Login forms, search boxes, comment fields, even parts of a web address can all reach a database — any one of them is a possible target if it isn't handled safely.

It's a decades-old, well-understood bug — and the fix has existed just as long. When it still shows up, it's almost always because that fix wasn't used somewhere.


Fighting Back

How developers defend against it

Real defenses stack a few layers, with one of them doing almost all of the actual work:

Parameterized queries

The real fix. The database is told the shape of the command and its values separately — "here's the command, and here's the value to slot in" — instead of mashing them into one string. A value can never turn into part of the command, no matter what characters it contains, because the database never re-reads it as code in the first place.

Input validation

A helpful second layer: reject or clean up input that doesn't look right for that field (a username shouldn't need a quote mark or a semicolon). This catches some mistakes, but on its own it's not the fix — attackers are good at finding characters a filter forgot to check.

Least-privilege database accounts

The login the app itself uses to talk to the database shouldn't be able to do more than the app actually needs — for example, it shouldn't be allowed to delete entire tables. If an injection does slip through, this limits how much damage it can do.

Code review

Having another developer read the code before it ships is one of the best ways a careless string-built query gets caught before real users are ever exposed to it — the same habit of checking each other's work before something goes live that shows up throughout security-minded teams.

Some companies also invite outside researchers to hunt for bugs like this on purpose, with permission, before an attacker finds them first — the same idea covered in What Is Ethical Hacking?


Your Turn

Check your understanding

In your own words: why do parameterized queries fix SQL injection, when "just being more careful" while building the query text by hand doesn't reliably work?

Your answer:

Sample answer: when a query is built by pasting user input directly into a string, the database can't tell the difference between "text the developer wrote" and "text a user typed" — both just become part of the same command, so a quote or a dash typed by a user can change what the command actually does. A parameterized query tells the database up front exactly which parts are the fixed command and which parts are values, before any user input is even attached. Because of that separation, a value is always treated as plain data — quotes, dashes, and all — no matter what's inside it. "Being careful" relies on a developer thinking of every dangerous character ahead of time; parameterized queries remove the danger structurally, so there's nothing left to be careful about.


Check Yourself

Quick quiz

1. What is SQL injection, at its core?

2. In username = 'INPUT', what does typing a single quote let an attacker do?

3. What actually closes an SQL injection hole (not just a helpful extra layer)?

Keep learning about cybersecurity

You've now seen how a single unchecked character can turn a harmless login box into a way into an entire database. Next, see what happens when the same "input isn't handled safely" idea gets aimed at a web page instead of a database.

Part of the Cybersecurity Basics tutorial series.