Lesson 16 · Data analytics zero to hero
SQL Fundamentals for Data Analysts | Data Analytics #16
Video sixteen of the 30-part series, and the start of a SQL block: SELECT, WHERE, ORDER BY, and aggregate functions. Entirely in Python, using the built-in…
- CourseData analytics zero to hero
- Lesson16 of 30
- Video12 min
- FormatJupyter notebook · 10 code cells
- Data1 dataset
What you'll learn
Datasets used in this lesson
Save these next to the notebook. In Google Colab, upload them with the 📁 icon on the left first.
- superstore_sales.csv715.5 KB
📓 Full notebook
Download .ipynbData Analytics Zero to Hero, Video 16: SQL Fundamentals for Data Analysts#
- Video sixteen of the 30-part series, and the start of a SQL block: SELECT, WHERE, ORDER BY, and aggregate functions.
- Entirely in Python, using the built-in sqlite3 module, no separate database software to install.
- We're loading the real Sample Superstore dataset from video fifteen into a genuine SQL database.
- 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 superstore_sales.csv in the same folder as this notebook.
- sqlite3 ships with Python; no installation needed.
Part 1: Loading Real Data into SQLite#
import sqlite3
import pandas as pd
conn = sqlite3.connect(':memory:')
df = pd.read_csv('superstore_sales.csv')
df.to_sql('sales', conn, if_exists='replace', index=False)
print('Real data loaded into the sales table')
result = pd.read_sql('SELECT * FROM sales LIMIT 5', conn)
print(result)
Part 2: SELECT and WHERE#
query = '''
SELECT "Order Date", Region, Category, Sales
FROM sales
WHERE Region = 'West'
LIMIT 5
'''
result = pd.read_sql(query, conn)
print(result)
query = '''
SELECT * FROM sales
WHERE Sales > 500 AND Category = 'Technology'
LIMIT 5
'''
result = pd.read_sql(query, conn)
print(result[['Region', 'Category', 'Sales', 'Profit']])
Part 3: ORDER BY and DISTINCT#
query = '''
SELECT "Sub-Category", Sales
FROM sales
ORDER BY Sales DESC
LIMIT 5
'''
result = pd.read_sql(query, conn)
print(result)
query = 'SELECT DISTINCT Region FROM sales'
result = pd.read_sql(query, conn)
print(result)
Part 4: Aggregate Functions#
query = '''
SELECT
COUNT(*) AS num_orders,
SUM(Sales) AS total_sales,
AVG(Sales) AS avg_sale,
MIN(Sales) AS min_sale,
MAX(Sales) AS max_sale
FROM sales
'''
result = pd.read_sql(query, conn)
print(result)
query = '''
SELECT Category, COUNT(*) AS num_orders, SUM(Sales) AS total_sales
FROM sales
GROUP BY Category
ORDER BY total_sales DESC
'''
result = pd.read_sql(query, conn)
print(result)
Part 5: The Same Question, Two Ways#
sql_answer = pd.read_sql("SELECT AVG(Profit) AS avg_profit FROM sales WHERE Category = 'Furniture'", conn)
pandas_answer = df[df['Category'] == 'Furniture']['Profit'].mean()
print(sql_answer)
print(pandas_answer)
conn.close()
print('Connection closed')
Wrap-Up: What You Learned#
- Loading a real DataFrame into a genuine SQL database with to_sql, and querying it back with read_sql.
- SELECT and WHERE, for choosing columns and filtering rows.
- ORDER BY and DISTINCT, for sorting and deduplicating results.
- Aggregate functions: COUNT, SUM, AVG, MIN, and MAX, plus a first look at GROUP BY.
- Answering the same real question in both SQL and pandas, landing on the same real answer.
- All of it run against the real Sample Superstore dataset, loaded into a genuine SQLite database. Video seventeen goes deep on SQL joins and aggregations, connecting multiple real tables the way video eleven did in pandas. 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.



