Lesson 18 · Data analytics zero to hero
Python + SQL: Real Analyst Queries | Data Analytics #18
Video eighteen of the 30-part series, and the last stop in the SQL block: subqueries, CTEs, window functions, and safe parameterized queries. Still the…
- CourseData analytics zero to hero
- Lesson18 of 30
- Video12 min
- FormatJupyter notebook · 9 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbData Analytics Zero to Hero, Video 18: Python + SQL, Building Analyst Queries#
- Video eighteen of the 30-part series, and the last stop in the SQL block: subqueries, CTEs, window functions, and safe parameterized queries.
- Still the real, multi-table Olist e-commerce dataset from the last two videos.
- Let's jump straight in.
Before You Start#
- Open a new Jupyter Notebook in VS Code and select your Python interpreter as the kernel.
- Place orders_sample.csv, customers_sample.csv, order_items_sample.csv, products_sample.csv, category_translation.csv, and payments_sample.csv in the same folder as this notebook.
import sqlite3
import pandas as pd
conn = sqlite3.connect(':memory:')
pd.read_csv('orders_sample.csv').to_sql('orders', conn, index=False, if_exists='replace')
pd.read_csv('customers_sample.csv').to_sql('customers', conn, index=False, if_exists='replace')
pd.read_csv('order_items_sample.csv').to_sql('items', conn, index=False, if_exists='replace')
pd.read_csv('products_sample.csv').to_sql('products', conn, index=False, if_exists='replace')
pd.read_csv('payments_sample.csv').to_sql('payments', conn, index=False, if_exists='replace')
print('All five real tables loaded')
Part 1: Subqueries#
query = '''
SELECT order_id, payment_value
FROM payments
WHERE payment_value > (SELECT AVG(payment_value) FROM payments)
ORDER BY payment_value DESC
LIMIT 5
'''
result = pd.read_sql(query, conn)
print(result)
query = '''
SELECT customer_id, customer_state
FROM customers
WHERE customer_id IN (
SELECT customer_id FROM orders WHERE order_status = 'delivered'
)
LIMIT 5
'''
result = pd.read_sql(query, conn)
print(result)
Part 2: Common Table Expressions with WITH#
query = '''
WITH order_totals AS (
SELECT order_id, SUM(price) AS order_total
FROM items
GROUP BY order_id
)
SELECT order_id, order_total
FROM order_totals
WHERE order_total > 200
ORDER BY order_total DESC
LIMIT 5
'''
result = pd.read_sql(query, conn)
print(result)
Part 3: Window Functions#
query = '''
SELECT customer_state, COUNT(*) AS num_customers,
RANK() OVER (ORDER BY COUNT(*) DESC) AS state_rank
FROM customers
GROUP BY customer_state
LIMIT 8
'''
result = pd.read_sql(query, conn)
print(result)
query = '''
SELECT order_id, price,
SUM(price) OVER (ORDER BY order_id) AS running_total
FROM items
ORDER BY order_id
LIMIT 6
'''
result = pd.read_sql(query, conn)
print(result)
Part 4: Parameterized Queries#
target_state = 'SP'
query = 'SELECT customer_id, customer_city FROM customers WHERE customer_state = ? LIMIT 5'
result = pd.read_sql(query, conn, params=(target_state,))
print(result)
Part 5: Handing a Query's Result to pandas#
query = '''
SELECT o.order_id, c.customer_state, i.price
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
JOIN items i ON o.order_id = i.order_id
'''
combined = pd.read_sql(query, conn)
state_summary = combined.groupby('customer_state')['price'].agg(['sum', 'mean', 'count']).sort_values('sum', ascending=False)
print(state_summary.head(5))
conn.close()
print('Connection closed')
Wrap-Up: What You Learned#
- Subqueries: scalar subqueries in WHERE, and IN with a subquery result set.
- Common Table Expressions with WITH, for naming a subquery and keeping complex real queries readable.
- Window functions: RANK and a running total with SUM OVER, computing per-row values without collapsing groups.
- Parameterized queries with ? placeholders, avoiding SQL injection when a query depends on a Python variable.
- Feeding a real SQL query's result straight into ordinary pandas analysis.
- This wraps up the SQL block. Video nineteen starts an Excel automation block: building real workbooks, formulas, and formatting entirely with Python. Subscribe so it lands automatically see you there.
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



