read_sql() takes either a raw SQL query string or a table name, plus a database connection object, commonly created with SQLAlchemy, or a DB-API connection like sqlite3, and returns the query's result set as a fully-formed DataFrame, with column names taken directly from the query's result columns. It's a convenient bridge between a relational database and pandas' data-analysis tools, letting you filter and join data at the database level with SQL before pulling only the data you actually need into memory.
1Understanding pd.read_sql()
read_sql() takes either a raw SQL query string or a table name, plus a database connection object, commonly created with SQLAlchemy, or a DB-API connection like sqlite3, and returns the query's result set as a fully-formed DataFrame, with column names taken directly from the query's result columns. It's a convenient bridge between a relational database and pandas' data-analysis tools, letting you filter and join data at the database level with SQL before pulling only the data you actually need into memory.
Always use parameterized queries when a read_sql() query includes variable, user-provided data — passing that data as separate params, not by formatting it directly into the SQL string, avoids SQL injection, the same rule that applies to raw database code.
import pandas as pd
import sqlite3
conn = sqlite3.connect("shop.db")
df = pd.read_sql("SELECT * FROM orders WHERE total > 100", conn)
print(df.shape)2Practical Example
Here is a real-world application of pd.read_sql() showing how it is used in production Pandas code.
import pandas as pd
import sqlite3
conn = sqlite3.connect("shop.db")
min_total = 100
df = pd.read_sql("SELECT * FROM orders WHERE total > ?", conn, params=(min_total,))
print(len(df))3Best Practices
Follow these guidelines when working with pd.read_sql():
1. Push filtering and joining logic into the SQL query itself when possible, rather than loading a whole table and filtering afterward in pandas, to reduce the amount of data transferred
2. Use parameterized queries, passing params separately, for any query built with variable data, to avoid SQL injection
3. Use a context-managed connection, or explicitly close it afterward, rather than leaving database connections open indefinitely
Tip: Always use parameterized queries when a read_sql() query includes variable, user-provided data — passing that data as separate params, not by formatting it directly into the SQL string, avoids SQL injection, the same rule that applies to raw database code.
import pandas as pd
import sqlite3
conn = sqlite3.connect("shop.db")
df = pd.read_sql("SELECT * FROM orders WHERE total > 100", conn)
print(df.shape)