Query data for analysis
Load filtered or aggregated database results into pandas for analysis.
Pandas · Function
Reads a SQL query result into a pandas DataFrame, optionally selecting an index, parsing dates, setting dtypes, or streaming rows in chunks. It accepts SQL text or a SQLAlchemy Selectable and can return a DataFrame iterator when chunksize is set.
The function executes a SQL query through a supported connection and converts its result set into a DataFrame. Optional arguments control indexing, numeric coercion, date parsing, chunked iteration, explicit dtypes, and the dtype backend.
pandas.read_sql_query(sql, con, index_col=None, coerce_float=True, params=None, parse_dates=None, chunksize=None, dtype=None, dtype_backend=<no_default>)| Name | Type | Description |
|---|---|---|
sql | str or SQLAlchemy Selectable | SQL query or SQLAlchemy Selectable (select or text object) to execute. |
con | SQLAlchemy connectable, str, or sqlite3 connection | Connection object or database location; SQLAlchemy connectable, URI string, or sqlite3 connection. |
index_col | str or list of str, optional | Column or columns to use as the DataFrame index. |
coerce_float | bool, default True | Attempt to convert suitable non-string, non-numeric objects to floating point. |
params | list, tuple, or mapping, optional | Values passed to the database driver's execute method. |
parse_dates | list-like, dict, or scalars, optional | Columns to parse as dates; supports column names, lists, or parsing dictionaries. |
chunksize | int, optional | Number of rows per returned DataFrame chunk; when set, returns an iterator. |
dtype | Type name or dict of column -> type, optional | Column dtype specification for the resulting DataFrame. |
dtype_backend | {‘numpy_nullable’, ‘pyarrow’}, default ‘numpy_nullable’ | Specifies the nullable-dtype backend used for the resulting DataFrame. |
return | DataFrame or Iterator[DataFrame] | The executed query result, as one DataFrame or an iterator of DataFrames when chunking is requested. |
DataFrame or Iterator[DataFrame]. The DataFrame contains the executed query's result set; with chunksize specified, an iterator yields DataFrame chunks.
import sqlite3
import pandas as pd
with sqlite3.connect(":memory:") as con:
con.executescript("CREATE TABLE users (id INTEGER, name TEXT); INSERT INTO users VALUES (1, 'Ada'), (2, 'Grace');")
result = pd.read_sql_query(
"SELECT id, name FROM users WHERE id > ?",
con,
params=(1,),
index_col="id",
)
print(result)Runs a parameterized SQLite query, uses the selected column as the DataFrame index, and prints the result.
Load filtered or aggregated database results into pandas for analysis.
Process large result sets without materializing every row at once.
Normalize database date and numeric values during ingestion.
Choose nullable NumPy or PyArrow dtypes at DataFrame creation.
It accepts a SQL string or SQLAlchemy Selectable and a SQLAlchemy connectable, URI string, or sqlite3 connection. The query result is returned as a DataFrame, unless chunksize is set, in which case iteration over DataFrames is returned.
Follow-up: How would you process a result too large for memory? Set chunksize and consume the returned DataFrame iterator incrementally.
index_col selects one or more result columns as the DataFrame index; without it, pandas uses the default integer index. A list of columns can create a MultiIndex.
Follow-up: What should be checked when choosing an index column? Verify that the selected result column or columns exist and have the intended uniqueness and ordering.
params passes values to the underlying execute method, but its accepted placeholder syntax depends on the database driver. The documentation points to DBAPI parameter styles and the driver documentation for the supported form.
Follow-up: Why should values not be interpolated into SQL text? Driver parameters preserve driver-specific binding behavior and avoid treating values as SQL syntax.
pandas.read_sqlpandas.read_sql_tablepandas.DataFramepandas.read_sql_query