# Parameterized Query

> A query with blank spots for the data. What a user types can never become a command.

**Field:** Software Development  
**Category:** Security  
**Also known as:** Prepared Statement, Bound Parameters, Placeholder Query

## Think of it like

Think of it like a form with boxes. Whatever you write in a box stays in that box.

## Definition

A parameterized query keeps the SQL command and the user's data apart. You write the query with blank spots, like ? or $1. The data is sent on its own and fills those spots. The database never treats that data as part of the command. This is the main defence against SQL injection, and almost every database library supports it.

## Example

Jack's login code used to glue the email into the SQL. He switches to a parameterized query, and a quote in an email is now just a quote.

## Example code

```python
cur.execute(
    "SELECT * FROM users WHERE email = %s",
    (email,),
)
```

The query has a blank spot. The email is sent apart from it and can never change the command.

## Why it matters

It shuts SQL injection at the root. Filters and escaping only guess at what is dangerous.

## Common mistake

Building the query with string formatting and then passing it in. The blank spots must be filled by the library, not by you.

## Three principles

- **Command and data apart.** They travel to the database separately.
- **Blank spots.** A ? or $1 marks where data goes.
- **The library fills it.** Never paste input in yourself.

## Related terms

- [SQL Injection](https://learn.benibix.com/term/development/sql-injection)
- [Injection Attack](https://learn.benibix.com/term/development/injection-attack)
- [Structured Query Language (SQL)](https://learn.benibix.com/term/development/sql)
- [Input Validation](https://learn.benibix.com/term/development/input-validation)
- [Object-Relational Mapping (ORM)](https://learn.benibix.com/term/development/orm)

## Told apart from

- [Input Validation vs Parameterized Query](https://learn.benibix.com/compare/input-validation-vs-parameterized-query): People think checking what users type is enough to stop SQL injection, and it is not. Input validation turns away data that has the wrong shape, like letters in an age box. But a name can still hold a quote. A parameterized query keeps every value apart from the command, so even odd text stays data. You want both.

![Three rows. In the first, SQL glued in, a query ends with the user's text x' OR '1'='1, and the result is that every user comes back. In the second, SQL kept apart, the query ends with a question mark and the same text is sent apart as data, so nothing matches. In the third, shell glued in, the line ping -c 1 example.com is followed by ; cat /etc/passwd, which runs a second command.](https://learn.benibix.com/diagrams/dev2-injection.svg)

---

Source: https://learn.benibix.com/term/development/parameterized-query
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.
