Most real business data lives inside databases, not CSV files. Python lets you connect directly to a database, run SQL queries, and pull results straight into Pandas for analysis — combining SQL's data-storage power with Python's analytical flexibility.
import sqlite3 # lightweight built-in database - perfect for learning
import pandas as pd
# For production databases: pip install sqlalchemy pymysql / psycopg2This module uses SQLite — a lightweight, file-based database built into Python, ideal for learning. The same concepts apply directly to MySQL, PostgreSQL, or SQL Server in real jobs, just with a different connector library.
import sqlite3
conn = sqlite3.connect("institute.db") # creates the file if it doesn't exist
cursor = conn.cursor()
print("Connected successfully!")| Database | Python Connector Library |
|---|---|
| SQLite | sqlite3 (built-in) |
| MySQL | pymysql / mysql-connector-python |
| PostgreSQL | psycopg2 |
| SQL Server | pyodbc |
| Any (via ORM) | sqlalchemy |
cursor.execute("""
CREATE TABLE IF NOT EXISTS students (
id INTEGER PRIMARY KEY,
name TEXT,
course TEXT,
fee INTEGER
)
""")
cursor.execute("INSERT INTO students VALUES (1, 'Priya', 'SEO', 12000)")
cursor.execute("INSERT INTO students VALUES (2, 'Rahul', 'Google Ads', 15000)")
conn.commit() # save changes to the databasecursor.execute("SELECT * FROM students WHERE fee > 12000")
results = cursor.fetchall()
for row in results:
print(row)Pandas can run a SQL query and load the results directly into a DataFrame in a single line — this is the most common way analysts pull data for analysis.
import pandas as pd
df = pd.read_sql_query("SELECT * FROM students", conn)
print(df)
print(df.describe())After cleaning or transforming data in Pandas, you can write it straight back into a SQL table.
df["fee_with_gst"] = df["fee"] * 1.18
df.to_sql("students_updated", conn, if_exists="replace", index=False)
print("Data written back to database successfully!")import sqlite3
import pandas as pd
conn = sqlite3.connect("institute.db")
query = """
SELECT course, SUM(fee) as total_revenue, COUNT(*) as student_count
FROM students
GROUP BY course
ORDER BY total_revenue DESC
"""
report = pd.read_sql_query(query, conn)
print(report)
conn.close() # always close the connection when doneBasic (5 Questions)
1. Connect to a new SQLite database file and create a students table.
2. Insert 5 rows of sample student data into your table.
3. Write a SELECT query to fetch all rows from your table.
4. Write a SELECT query with a WHERE clause to filter by course.
5. Load your entire table into a Pandas DataFrame using read_sql_query().
Intermediate (5 Questions)
1. Write a query using GROUP BY to calculate total fee per course directly in SQL.
2. Load a table into Pandas, clean/transform it, and write it back using to_sql().
3. Use an UPDATE query to change one student's course, then verify the change.
4. Use a DELETE query to remove one row, then confirm using a SELECT.
5. Combine a SQL query with a Pandas groupby() to double-check the same aggregation both ways.
Advanced (5 Questions)
1. Build a small 'Institute Database' with 2 related tables (students, payments) and write a JOIN query between them.
2. Load data using a JOIN query directly into Pandas and calculate outstanding balances per student.
3. Build a Python function run_report(query) that takes any SQL string and returns a clean Pandas DataFrame.
4. Write a script that reads a CSV, cleans it in Pandas, and loads the clean version into a new SQL table.
5. Build a simple 'Revenue Dashboard Query Tool' that lets you type a course name and returns that course's total revenue from the database.
Q1. Which Python module provides a built-in lightweight database?
Q2. Which command saves changes after an INSERT/UPDATE/DELETE?
Q3. Which Pandas function loads SQL query results directly into a DataFrame?
Q4. Which function writes a DataFrame back into a SQL table?
Q5. What should you always do after finishing database operations?
Assignment: Institute Database Revenue Report
Expected Output: A complete pipeline showing data flowing from SQL, into Pandas for transformation, and back out as both a database table and shareable files.
Use this space to write down key points, doubts, and your own examples from today's session.