Tutorials › Python › SQL with Python (sqlite3 and SQLAlchemy)
SQL with Python (sqlite3 and SQLAlchemy)
Query databases from Python and load results directly into pandas DataFrames
Most company data lives in databases, not CSV files. As a data analyst, you will frequently need to: connect to a SQL database from Python, run a query, and load the result into a pandas DataFrame for further analysis.
Python's sqlite3 (built-in) handles SQLite databases. SQLAlchemy + pandas handles SQL Server, MySQL, PostgreSQL, and other enterprise databases used by companies in Noida and Delhi NCR.
Examples
Connect to SQLite and load into pandas
import sqlite3
import pandas as pd
# Connect to SQLite database
conn = sqlite3.connect("company_data.db")
# Run a query and load into DataFrame
query = """
SELECT
c.CustomerName,
c.Region,
SUM(o.Amount) AS TotalRevenue,
COUNT(o.OrderID) AS OrderCount
FROM Orders o
JOIN Customers c ON o.CustomerID = c.CustomerID
WHERE o.OrderDate >= '2026-01-01'
GROUP BY c.CustomerName, c.Region
ORDER BY TotalRevenue DESC
"""
df = pd.read_sql_query(query, conn)
conn.close()
print(df.head(10))
Connect to SQL Server / MySQL with SQLAlchemy
from sqlalchemy import create_engine
import pandas as pd
# SQL Server (common in Noida IT companies)
engine = create_engine(
"mssql+pyodbc://username:password@server_name/database_name?driver=ODBC+Driver+17+for+SQL+Server"
)
# MySQL
engine = create_engine("mysql+pymysql://username:password@host/database")
# Run query → DataFrame in one line
df = pd.read_sql("SELECT * FROM Sales WHERE Region = 'NCR'", engine)
# Write DataFrame to database table
df_clean.to_sql("cleaned_sales", engine, if_exists="replace", index=False)
# if_exists: "replace" (drop and recreate), "append" (add rows), "fail" (error if exists)
print(f"Loaded {len(df):,} rows from SQL Server")
💡 pd.read_sql_query() is the most important function here — it combines SQL querying and pandas loading in one step.
Key Points
✓pd.read_sql_query(query, connection) runs SQL and returns a DataFrame directly
✓Always close() the connection or use a context manager (with sqlite3.connect() as conn)
✓SQLAlchemy engine is the universal connector — same pandas code works for any DB type
✓df.to_sql() writes a DataFrame back to a database table — useful for pipeline outputs