SQL for Analytics I — Select, Filter, Group

P12.data-quality-sql.01 · Audience: guest, it-ml, language-pro · Prerequisites: From Notebook to Pipeline

Before a number can be trusted it has to be computed, and for most of the world's analytics that computation is written in SQL — a language old enough to vote several times over and still the first tool an analyst reaches for. Its whole design is one idea: describe the rows you want, and let the engine find them. This module builds that idea from three verbs — SELECT to choose columns, WHERE to keep rows, GROUP BY to fold many rows into one summary — on a tiny retail dataset small enough to check every answer by hand. Everything runs in the scratch console below, which ships a real SQLite engine; nothing is fetched, nothing is hidden.

Step 1 / 5Three tables, one question

Everything in this track runs on one small retail dataset: three tables that between them record who our customers are, what we sell, and what got ordered. It is deliberately tiny — every total below can be checked on your fingers — but it has the exact shape of a real warehouse: facts (the orders) pointing at dimensions (the customers and products they refer to).

customerscustomer_idcity
c1Paris
c2Lyon
productsproduct_idprice
p110.0
p220.0
ordersorder_idcustomer_idproduct_idquantity
o1c1p12
o2c1p21
o3c2p13

The question we will chase all module is a business one: how much revenue does each city bring in? Nothing in any single table answers it — the money lives in orders (as quantities), the prices in products, the cities in customers. SQL's job is to describe the answer we want and let the engine assemble it.

ⓘ Concept: SQL is declarative: say what, not how
query=adescriptionofthedesiredrowsengineplansandexecutestheretrievalquery = a description of the desired rows → engine plans and executes the retrieval

Why it matters — A SQL query does not spell out loops or index lookups — it names the rows and columns you want, and the database's query planner decides how to fetch them. That is the whole reason one language has survived fifty years of hardware: you describe the result, and the engine, not you, worries about the mechanics. Learn to state the 'what' precisely and SQL rewards you; try to micromanage the 'how' and you are fighting the tool.

Ask the mentor about this module

Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.

Ctrl/Cmd + Enter to send

Try it yourself

A scratch console for this page's ideas — ungraded, nothing you run here is recorded.

Scratch console

A scratch console with the scientific stack (pandas, numpy, scikit-learn). Runs on the server — no network, resource-limited and measured.

Output appears here.