Lesson 17 · Data analytics zero to hero
SQL Joins & Aggregations Explained | Data Analytics #17
Video seventeen of the 30-part series: JOIN, GROUP BY, and HAVING, combining multiple real tables the way real analyst SQL usually looks. We're back to the…
- CourseData analytics zero to hero
- Lesson17 of 30
- Video12 min
- FormatJupyter notebook · 8 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 17: SQL Joins and Aggregations#
- Video seventeen of the 30-part series: JOIN, GROUP BY, and HAVING, combining multiple real tables the way real analyst SQL usually looks.
- We're back to the real, multi-table Olist Brazilian E-Commerce extract from video eleven, this time queried with SQL instead of pandas merge.
- 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.
Part 1: Loading Multiple Real Tables into SQLite#
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('category_translation.csv').to_sql('translation', conn, index=False, if_exists='replace')
print('All five real tables loaded')
Part 2: INNER JOIN#
query = '''
SELECT o.order_id, o.order_status, c.customer_state
FROM orders o
INNER JOIN customers c ON o.customer_id = c.customer_id
LIMIT 5
'''
result = pd.read_sql(query, conn)
print(result)
Part 3: LEFT JOIN#
query = '''
SELECT i.order_id, i.product_id, p.product_category_name, i.price
FROM items i
LEFT JOIN products p ON i.product_id = p.product_id
LIMIT 5
'''
result = pd.read_sql(query, conn)
print(result)
query = '''
SELECT
COUNT(*) AS total_items,
SUM(CASE WHEN p.product_id IS NULL THEN 1 ELSE 0 END) AS unmatched_items
FROM items i
LEFT JOIN products p ON i.product_id = p.product_id
'''
result = pd.read_sql(query, conn)
print(result)
Part 4: Joining Three Real Tables at Once#
query = '''
SELECT o.order_id, c.customer_state, p.product_category_name, 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
JOIN products p ON i.product_id = p.product_id
LIMIT 5
'''
result = pd.read_sql(query, conn)
print(result)
Part 5: GROUP BY Across a Join#
query = '''
SELECT t.product_category_name_english AS category, COUNT(*) AS num_items, ROUND(SUM(i.price), 2) AS total_revenue
FROM items i
JOIN products p ON i.product_id = p.product_id
JOIN translation t ON p.product_category_name = t.product_category_name
GROUP BY t.product_category_name_english
ORDER BY total_revenue DESC
LIMIT 5
'''
result = pd.read_sql(query, conn)
print(result)
Part 6: HAVING#
query = '''
SELECT customer_state, COUNT(*) AS num_orders
FROM customers
GROUP BY customer_state
HAVING COUNT(*) >= 20
ORDER BY num_orders DESC
'''
result = pd.read_sql(query, conn)
print(result)
conn.close()
print('Connection closed')
Wrap-Up: What You Learned#
- Loading several real CSVs into separate tables in one shared SQLite database.
- INNER JOIN, for keeping only matched rows across two real tables.
- LEFT JOIN, for keeping every row from the first table, and checking for unmatched rows with CASE WHEN.
- Chaining multiple JOIN clauses to combine three or more real tables in one query.
- GROUP BY after a join, and HAVING for filtering on aggregated results.
- All of it run against the real, multi-table Olist e-commerce dataset. Video eighteen puts SQL and Python together directly: subqueries, and passing query results straight into pandas for further real analysis. 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.



