# SQL Injection

> An attack that hides database commands in a form. The app then runs them.

**Field:** Software Development  
**Category:** Security  
**Also known as:** SQLi, SQL Injection Attack

## Think of it like

Think of it like a form that asks your name. You write your name, then add: and open the safe.

## Definition

SQL injection hides database commands in a form, so the app runs them. It happens when code glues user input straight into an SQL query. Typing ' OR '1'='1 into a login box can make the query true for every user. Attackers can then read, change or delete the whole database. The fix is a query with placeholders. It keeps input as data, never as code.

## Example

Jack's search box builds its query by joining strings. Someone types a quote and a DROP TABLE command, and his products table is gone.

## Example code

```sql
SELECT * FROM users
WHERE name = '' OR '1'='1';
```

The typed text turned the check into one that is always true.

## Why it matters

It is one of the oldest web attacks and still among the most damaging. One open query can leak every row.

## Common mistake

Trying to clean input by taking out quotes. Use queries with placeholders instead; they fix it for good.

## Three principles

- **Input becomes code.** That is the flaw.
- **The whole database.** Is at risk.
- **Placeholders.** Keep input as data.

## Related terms

- [Input Validation](https://learn.benibix.com/term/development/input-validation)
- [Structured Query Language (SQL)](https://learn.benibix.com/term/development/sql)
- [OWASP Top 10](https://learn.benibix.com/term/development/owasp-top-10)
- [Cross-Site Scripting (XSS)](https://learn.benibix.com/term/development/cross-site-scripting)
- [Parameterized Query](https://learn.benibix.com/term/development/parameterized-query)

---

Source: https://learn.benibix.com/term/development/sql-injection
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
