Module · 2 lessons

Querying Databases from Python

Bring SQL into your Python workflow — connect to a database, run queries with sqlite3, and load results into pandas.

At a glance

Level
Intermediate
Lessons
2 lessons
Time to complete
1 week
Cost
Free forever · no sign-up

Welcome to Querying Databases from Python — the module where two of the most important skills in data work finally meet. You already know how to write SQL to ask a database questions, and you have seen how Python lets you clean, reshape, and visualize data. Here you connect the two so that a single Python script can reach into a database, pull back exactly the rows you need, and hand them straight to your analysis tools.

Data for this module

Databases: jobs.db — U.S. recent-graduate job statistics (from FiveThirtyEight), and world_population.db — country population data (both SQLite).

Download the one each lesson needs and place it in your working directory. Each lesson lists the specific table it uses.

You will work with the lightweight sqlite3 library that ships with Python, so there is nothing to install. You will open a connection to a database file, create a cursor to run queries, and learn how SQLite returns each row as a tuple. From there you will move up a level and load query results directly into a pandas DataFrame, where filtering, grouping, and plotting all become natural.

Everything is hands-on. You will run real queries against real database files and read the actual output, so by the end you will not just understand the workflow — you will be using it to answer real questions about college majors and world population data.

No setup beyond Python is required, and a little SQL goes a long way. Start with Lesson 1, where you will open your first database connection and run a query from Python.

Lessons in this module

Achievement

Complete all 2 lessons to finish the Querying Databases from Python module.

Start module