October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

How to Read SQL Query Results into a Python DataFrame

Use pandas read_sql_query to turn SQL results into a DataFrame, with a simple sqlite3 example and a SQLAlchemy route for server databases.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use pandas’ read_sql_query(sql, con) to run a SQL query and receive its rows as a DataFrame. For a local SQLite database, Python’s built-in sqlite3 module is the simplest route. For PostgreSQL and other server databases, use the database’s driver with a SQLAlchemy connection, or an appropriate ADBC 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 Included with Python, and pandas supports its connections directly. SQLite’s SQL and type behavior can differ from server databases. pandas read_sql documentation
SQLAlchemy plus a database driver PostgreSQL or another database supported by the appropriate SQLAlchemy dialect and DBAPI driver Install the driver for your database, use the correct URL format, and manage the connection or engine lifecycle. SQLAlchemy engine documentation
ADBC connection A database with an appropriate ADBC driver, particularly when native-type or high-performance I/O is useful Driver and database support vary; verify compatibility in the current pandas and ADBC documentation. pandas read_sql documentation

The pandas API accepts ADBC connections, SQLAlchemy connectables, and sqlite3 connections. For a DB-API connection other than SQLite, the documented general integration path is SQLAlchemy. A generic DB-API connection is not necessarily interchangeable with a documented SQLAlchemy connection.

As an Amazon Associate I earn from qualifying purchases.

Read a local SQLite database into a DataFrame

Open the database file with sqlite3.connect(), then pass the connection and a SQL 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 file and table. The with block closes the connection when execution leaves the block. For SQLite specifically, pandas documents that read_sql() takes a SQL query; passing only a table name results in an error. See the pandas read_sql documentation.

Connect to a server database with SQLAlchemy

Install pandas, SQLAlchemy, and the DBAPI driver for your database. Then create an engine with the URL format required by that database and driver. For example, SQLAlchemy documents PostgreSQL URLs using the psycopg2 driver; the exact URL and driver depend on the backend. Check SQLAlchemy’s engine documentation for URL formats.

import pandas as pd
from sqlalchemy import create_engine, text

# Set database_url through your application's secret-management approach.
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 your application supplies; don’t put real credentials directly in source code. The example uses a SQLAlchemy text() query with a named parameter. Close connections according to their lifecycle, and dispose of an engine when your application is finished with it. pandas assigns callers responsibility for closing SQLAlchemy connectables and disposing of engines; it closes string connection inputs automatically. pandas connection lifecycle guidance

Bind query values safely

Pass values separately with the params argument rather than building SQL by interpolating untrusted input. The placeholder syntax depends on the database driver; use the convention it supports. pandas warns that it does not sanitize SQL statements and forwards them to the underlying driver, which may or may not sanitize them. Read pandas’ SQL execution guidance.

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

In the SQLAlchemy example above, :status is a named bind parameter and {"status": "active"} supplies its value. Keep SQL structure separate from data values; do not treat params as a way to insert arbitrary table or column names.

Shape and handle the query results

  • Read only what you need: Select relevant columns and filter rows in SQL to avoid bringing unnecessary data into Python.
  • Process large results in chunks: Set chunksize to receive an iterator of DataFrames instead of loading the entire result into one DataFrame. Choose a chunk size based on row width, query, and available memory; pandas does not prescribe a universal value.
  • Convert dates deliberately: Use parse_dates when date conversion is needed, and check the documented formats and behavior for your data.
  • Control the DataFrame shape and types: The API also supports index_col and dtype-related options. Verify types explicitly when downstream analysis relies on them.

For the full set of arguments and supported connection types, see the pandas read_sql API.

Troubleshoot connection and result errors

  • Driver or URL mismatch: Confirm the installed driver matches the SQLAlchemy URL and selected database dialect. SQLAlchemy’s engine documentation describes URL forms.
  • Connection failure: Check credentials, host, port, database name, and network access for the environment you are connecting from. These settings are database- and deployment-specific.
  • Unsupported connection object: Pass a connection type pandas documents for your route. For server databases, use a SQLAlchemy connectable or a supported ADBC connection rather than assuming any DB-API object will work.
  • Parameter error: Match placeholders to the driver’s syntax and provide the corresponding values through params.
  • Memory pressure: Reduce the result in SQL or use chunksize to iterate over DataFrames.
  • Unexpected date or dtype values: Inspect the resulting DataFrame’s types and configure date parsing or dtype options as needed.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Write a DataFrame back to SQL

To send DataFrame records in the other direction, use DataFrame.to_sql() with a supported connection. Its API warns that it does not sanitize inputs supplied through to_sql, so treat names and other inputs carefully and consult the underlying driver guidance. pandas to_sql documentation

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.

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

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.