Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use pandas.read_sql_query(sql, connection) to run a SQL query and receive its rows as a pandas DataFrame. For a local SQLite database, Python’s built-in sqlite3 module is enough. For PostgreSQL and other server databases, use the appropriate database driver, commonly through a SQLAlchemy engine or connection.

Choose the connection method for your database

Connection path Best suited to What to know
sqlite3 A local SQLite file or an in-memory SQLite database. Python includes the module, and pandas supports its connections directly. SQLite has its own SQL and type behavior. pandas SQL queries documentation
SQLAlchemy engine or connection with a database driver PostgreSQL and other databases supported by a SQLAlchemy dialect and DBAPI driver. Install a driver that matches the database and URL, and manage the connection or engine lifecycle. Check SQLAlchemy’s documentation for the selected backend’s URL format. SQLAlchemy engine documentation
ADBC connection A database with a suitable ADBC driver, particularly when native types or high-performance I/O are useful. Available drivers and database support vary; check current pandas and ADBC documentation. pandas SQL queries documentation

pandas documents support for ADBC connections, SQLAlchemy connectables, and sqlite3 connections. For a DB-API connection other than sqlite3, its general integration route is SQLAlchemy. pandas SQL queries documentation

Read a SQLite query into a DataFrame

For a SQLite database, open a connection with sqlite3.connect(), then pass it and your query to read_sql_query():

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import sqlite3
import pandas as pd

with sqlite3.connect("example.db") as con:
    df = pd.read_sql_query(
        "SELECT id, name FROM customers",
        con,
    )

print(df.head())

Replace example.db and the query with your database file and table or columns. The result, df, is a DataFrame. In pandas’ SQLite example, read_sql expects a SQL query; passing only a table name raises an error. pandas SQL queries documentation

Connect to a server database with SQLAlchemy

For PostgreSQL or another server database, install pandas, SQLAlchemy, and the DBAPI driver for the database. Create an engine using the URL format and driver required by that backend. SQLAlchemy documents, for example, PostgreSQL URLs that use the psycopg2 driver. SQLAlchemy engine documentation

import pandas as pd
from sqlalchemy import create_engine, text

# Set database_url through your environment or secret-management system.
engine = create_engine(database_url)

query = text("SELECT id, name FROM customers WHERE status = :status")
with engine.connect() as conn:
    df = pd.read_sql_query(query, conn, params={"status": "active"})

print(df.head())

database_url represents a value you supply for your database; keep credentials out of source code in real applications. Close connections when finished, and dispose of an engine when its lifecycle ends. pandas notes that callers are responsible for disposing of an engine and closing an ADBC connection or SQLAlchemy connectable; string connection inputs are closed automatically. pandas SQL queries documentation

Pass query values safely

Use the params argument to bind values rather than inserting untrusted input into SQL text. The parameter placeholder syntax depends on the database driver; the SQLAlchemy example above uses a named parameter in text(). pandas SQL queries documentation

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

pandas warns that it does not sanitize SQL statements: it forwards the statement to the underlying driver, which may or may not sanitize it. Parameter binding helps keep values separate from the query, but it does not make arbitrary SQL text safe. pandas SQL queries documentation

Shape the result for analysis

Read large results in chunks

Pass chunksize to receive an iterator of DataFrames instead of loading the entire result into one DataFrame. Pick a chunk size based on the query, row width, and available memory; pandas does not prescribe a universal value. pandas SQL queries documentation

Handle dates, indexes, and types

Use parse_dates when you want pandas to convert date columns, and consult its documented formats and behavior for the data being returned. The API also provides index_col and dtype-related options. Check the resulting column types when downstream analysis depends on exact date or dtype handling. pandas SQL queries documentation

Use the matching read function

read_sql_query(sql, con) is explicit about reading a SQL query. read_sql(sql, con) can also read a query and, where supported, a table name; SQLite’s documented behavior is a notable exception to table-name-only use. pandas SQL queries documentation

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot connection and result errors

  • With SQLAlchemy, confirm the database driver is installed and matches the URL’s dialect and driver.
  • Check that the credentials, host, port, database name, and network access are correct for your target environment.
  • Pass a connection type supported by pandas for the chosen route; a generic DB-API connection is not necessarily a documented connectable for every backend.
  • Match parameter placeholders to the driver and provide values through params.
  • For a large result, select only the needed columns and rows in SQL, then consider chunked reads.
  • If dates or other column types matter to later calculations, inspect and explicitly configure their conversion.

For writing DataFrame rows back to a database, pandas provides DataFrame.to_sql(). Its API warns that it does not sanitize inputs supplied through to_sql, so handle table names and other inputs carefully and consult the underlying driver guidance. pandas DataFrame.to_sql API

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.