SQL and Python Integration

  • ID: DBSQL-F-L06
  • Type: Lesson
  • Audience: Public
  • Theme: Query then analyze

SQL and Python work best together when responsibilities are clear.

SQLite engine behavior is documented in the official documentation SQLite Consortium (2026).


Setup


Revenue per Customer

query = '''
SELECT c.full_name,
       SUM(oi.quantity * oi.unit_price) AS revenue
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
JOIN order_items oi ON o.order_id = oi.order_id
GROUP BY c.full_name
ORDER BY revenue DESC;
'''

df = pd.read_sql(query, con)
print(df)
       full_name  revenue
0  Joseph Kimaro    25.75
1   Amina Hassan    19.50
2  Grace Nyerere    17.80
3    Neema Msuya     2.50

Visualize the Summary

plt.bar(df["full_name"], df["revenue"])
plt.xticks(rotation=45)
plt.title("Revenue by Customer")
plt.tight_layout()
plt.show()


Why SQL First Matters

A common workflow mistake is to load full tables into Python and then join or filter there.

Better practice is to:

  • Filter in SQL
  • Join in SQL
  • Aggregate in SQL
  • Transfer only structured results into Python

This keeps analysis efficient and reduces opportunities for structural mistakes.