Python Central HubSign in

Data Analytics with Python / Phase 9: SQL for Data Analytics

Introduction to SQL for Data Analytics

Learn SQL fundamentals for analytics - tables, queries, filtering, grouping, and joining data.

Why SQL is essential

Most analytics work happens where the data lives:

  • Relational databases (PostgreSQL, MySQL, SQLite)
  • Data warehouses (BigQuery, Snowflake, Redshift)

SQL is used to:

  • Retrieve data efficiently
  • Aggregate metrics (DAU, revenue, churn)
  • Join multiple tables (users + orders + events)

Core mental model

  • Data is stored in tables.
  • A query reads rows/columns and returns a result set.
SQL
SELECT *
FROM users
LIMIT 10;

Typical analytics tables

  • users(user_id, created_at, country, plan)
  • orders(order_id, user_id, order_ts, amount)
  • events(user_id, event_ts, event_name, device)

What you’ll learn in this phase

  • SELECT, WHERE, ORDER BY, LIMIT
  • Aggregations: COUNT, SUM, AVG
  • GROUP BY, HAVING
  • Joins
  • Window functions
  • CTEs
  • Using SQL from Python (pandas)

Where SQL fits in an analytics workflow

You rarely run one query in isolation. The usual shape of the work is: raw tables live in a database, a query filters and reshapes them, and the result lands somewhere you can chart, model, or share.

diagram Where SQL fits in analytics mermaid
Raw tables live in a database; a query filters and shapes them into a result you can chart or model.

The database never changes because you queried it — a SELECT just reads and returns rows. That's what makes SQL safe to explore with: you can run the same query a hundred times while you're figuring out the right filter, and nothing gets damaged.

SQLite: a database in a single file

You don't need a server to practice SQL. SQLite (Python's built-in sqlite3 module) stores an entire database in one file — or even in memory — which makes it perfect for learning and for the exercises on this page.

Python
import sqlite3
 
conn = sqlite3.connect(":memory:")
conn.execute("CREATE TABLE users (user_id INTEGER, country TEXT, plan TEXT)")
conn.executemany(
    "INSERT INTO users VALUES (?, ?, ?)",
    [(1, "IN", "free"), (2, "IN", "pro"), (3, "US", "pro")],
)
conn.commit()
 
rows = conn.execute("SELECT * FROM users").fetchall()
print(rows)
Python
[(1, 'IN', 'free'), (2, 'IN', 'pro'), (3, 'US', 'pro')]

Check your understanding

quizCheck yourself
  1. What does a SELECT query do to the database?

    Show answer

    A — It only reads and returns rows, leaving the data unchanged — That is why SQL is safe to explore: you can run the same query many times.

  2. Which SQLite call creates a throwaway in-memory database in the lesson?

    Show answer

    A — sqlite3.connect(":memory:") — No file or server is needed, and nothing is left behind.

  3. What does SELECT * FROM users LIMIT 10 return?

    Show answer

    A — Ten users at most, with all columns — The star selects every column and LIMIT caps the number of rows.

  4. Which table in the lesson holds amount and order_ts?

    Show answer

    B — orders — orders has order_id, user_id, order_ts and amount, while events holds event_name and device.

🧪 Try It Yourself

Exercise 1 – Create a table and read it back

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Filter rows with WHERE

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Load a query result into pandas

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Next

Head to SQL Basics (SELECT, WHERE, ORDER BY, LIMIT) to practice the four clauses you'll use in almost every query you ever write.

Was this page helpful?

Let us know how we did