parameterized query
Parameterized queries use placeholders instead of embedded values to prevent SQL injection and improve performance. Learn named vs positional parameters with Python examples.
Overview
A parameterized query is a SQL statement that uses placeholders instead of directly embedding values into the query text. These placeholders get replaced with actual values when the query executes, making queries both safer and more reusable. This is a fundamental concept in preventing SQL injection attacks and improving query performance through plan caching.
DuckDB Implementation
In DuckDB, parameterized queries can be written using either named parameters prefixed with $ or positional parameters using ?. The named parameter style is generally preferred as it makes queries more readable and less prone to ordering errors.
Named parameters example:
Copy code
SELECT * FROM users
WHERE age > $min_age
AND country = $country;
Positional parameters example:
Copy code
SELECT * FROM users
WHERE age > ?
AND country = ?;
Usage in Practice
When using DuckDB through its Python API, you can pass parameters as a dictionary for named parameters or as a tuple/list for positional parameters:
Copy code
# Named parameters
conn.execute("SELECT * FROM users WHERE age > $min_age",
{"min_age": 21})
# Positional parameters
conn.execute("SELECT * FROM users WHERE age > ?",
(21,))
Benefits
Parameterized queries provide several key advantages:
- Protection against SQL injection by properly escaping and quoting values
- Better performance through query plan caching
- Cleaner and more maintainable code by separating the query logic from the data values
- Ability to reuse the same query structure with different parameter values
Differences from Other Databases
While most databases support parameterized queries, the syntax varies. DuckDB's $ prefix for named parameters is similar to PostgreSQL, while databases like MySQL use ? for all parameters and Microsoft SQL Server uses @ for named parameters. DuckDB's support for both styles makes it more flexible while maintaining compatibility with common patterns.
Related terms
A query is a request made to a database to retrieve, modify, or analyze data.
SQL query →A SQL query is a structured request written in Structured Query Language (SQL) that allows you to retrieve, analyze, or manipulate data stored in a database.
query engine →A query engine parses, optimizes, and executes SQL queries against data sources. Learn its core architecture, how it differs from a database, and see examples like DuckDB and Presto.
SQL →SQL (Structured Query Language) is the standard language for working with relational databases.
FAQS
A parameterized query is a SQL statement that uses placeholders instead of embedding values directly in the query text. The placeholders are replaced with actual values at execution time, making queries safer against SQL injection and more reusable.
DuckDB supports named parameters prefixed with $ (e.g. WHERE age > $min_age) or positional parameters using ?. Named parameters are preferred for readability. From the Python API, pass a dict for named parameters or a tuple/list for positional ones.
They protect against SQL injection by properly escaping values, improve performance through query-plan caching, keep code cleaner by separating query logic from data, and let you reuse one query structure with different values.


