Skip to content
Module 01 of 1230 min readBeginner

Why SQL, and how a database thinks

The mental model: relations, rows, and why declarative beats imperative for data.

8%

Listen along

Read “Why SQL, and how a database thinks” aloud

Plays in your browser using on-device text-to-speech — nothing leaves the page.

SQL stands for Structured Query Language. It's the language you use to ask questions of relational databases — and it's been the industry standard for forty years for one reason: it's fundamentally good at the problem.

The mental model

Think of a database as a collection of tables. Each table is a grid: rows are records, columns are fields. SQL lets you slice, filter, join, and summarize that grid using a small vocabulary of verbs.

The key insight: SQL is declarative. You describe WHAT you want, not HOW to get it. The database engine figures out the most efficient path. That's a remarkable abstraction — it lets a beginner write the same query as an expert and (often) get the same speed.

Declarative vs imperative

In Python or JavaScript, you say: 'loop over the list, check each item, append matching ones to a result.' In SQL, you say: 'give me the rows where X is true.' The engine handles the loop.

What you'll be able to do by the end of this course

  • Pull any subset of rows from a database with confidence
  • Combine information from multiple tables (the JOIN — the single skill that separates 'I know SQL' from 'I really know SQL')
  • Roll up to summaries: revenue by month, customers by country, retention by cohort
  • Use window functions to compute running totals, ranks, and trailing averages
  • Read someone else's query and improve it

Setting up to follow along

You don't need to install anything yet. Many of the queries in this course will run in any browser-based SQL playground (sqlite.org/playground.html, db-fiddle.com, sqlfiddle.com). When we get to module 5, we'll move to a real database.

The fastest local setup

DuckDB is a single-file analytical database that runs anywhere. Install it (brew install duckdb on Mac, or download from duckdb.org), and you have a Postgres-grade SQL engine with no server to configure. We use it in modules 9-11.

Exercise

An analyst is debating whether to learn SQL or stick with Excel. Their daily work involves filtering a 50,000-row spreadsheet of M-Pesa transactions, computing customer-level totals, and producing monthly summary reports. (1) For each task, identify whether SQL or Excel is the better tool and why. (2) At what dataset size does Excel start to break? (3) Beyond capacity, what does SQL offer that Excel does not? (4) The analyst worries SQL is harder to learn — frame the realistic learning curve.

Loading progress…
LeadAfrikPublic Economics Hub