TutorialsPythonSQL with Python — pandas and SQLite

SQL with Python — pandas and SQLite

Query databases directly from Python using pandas and run SQL on DataFrames with DuckDB

Data analysts often need to pull data from SQL databases into Python for further analysis. You can query SQL Server, MySQL, PostgreSQL, and SQLite from Python — loading results directly into Pandas DataFrames. You can also run SQL queries ON Pandas DataFrames using DuckDB or pandasql — letting you use familiar SQL syntax on in-memory data.

Examples

Query SQL databases from Python
import pandas as pd
import sqlalchemy

# Connect to SQL Server (most common in Noida companies)
engine = sqlalchemy.create_engine(
    "mssql+pyodbc://username:password@server/database?driver=ODBC+Driver+17+for+SQL+Server"
)

# Run a query and load to DataFrame
query = """
SELECT
    Region,
    SUM(Amount) AS Total_Sales,
    COUNT(*) AS Order_Count
FROM Sales
WHERE OrderDate >= '2026-01-01'
GROUP BY Region
ORDER BY Total_Sales DESC
"""

df = pd.read_sql(query, engine)
print(df)

# SQLite (local database, no server needed — great for learning)
import sqlite3
conn = sqlite3.connect("local_data.db")
df = pd.read_sql("SELECT * FROM sales LIMIT 1000", conn)
conn.close()

# Write DataFrame back to SQL
df_clean.to_sql("clean_sales", engine, if_exists="replace", index=False)
# if_exists: "replace" overwrites, "append" adds rows, "fail" errors if table exists
Run SQL on DataFrames with DuckDB
import duckdb
import pandas as pd

df = pd.read_csv("sales.csv")

# Run SQL directly on the DataFrame!
result = duckdb.query("""
    SELECT
        Region,
        SUM(Amount) AS Total,
        AVG(Amount) AS Average,
        COUNT(*) AS Orders
    FROM df
    WHERE Amount > 10000
    GROUP BY Region
    ORDER BY Total DESC
""").df()   # .df() converts result to Pandas DataFrame

print(result)

# pip install duckdb — lightweight, fast, no server needed
# Perfect for analysts who prefer SQL but need Python's ecosystem
💡 DuckDB is one of the fastest tools for running SQL on CSV/Parquet files. It is becoming very popular for data analysis in 2026.

Key Points

  • pd.read_sql(query, connection) loads SQL query results directly into a DataFrame
  • sqlalchemy.create_engine() connects to SQL Server, MySQL, PostgreSQL
  • DuckDB runs SQL directly on Pandas DataFrames — no database server needed
  • df.to_sql() writes a DataFrame to a database table
  • Combining SQL + Python is the most powerful analyst stack: SQL for extraction, Python for analysis

Practice Question

Which function loads the results of a SQL query directly into a Pandas DataFrame?

Related Topics

Merging DataFramesCombine DataFrames with merge and concat — the Pandas equivalent of SQL JOINsgroupby and Pivot TablesAggregate data by category with groupby — the Pandas equivalent of Excel pivot tablesReading CSV and Excel FilesLoad data from CSV, Excel, and multiple sheets into Pandas DataFrames