Applied edition · 44 planned hours · Unpiloted · Human review pending. Self-review is not a credential; the full 108-hour semester remains unfinished. No browser SQL execution.

Download Markdown

Unit 1 — Ask a question a table can answer

Outcome U1: define the grain, population and keys of a small relational analysis; justify a two-table model without repeating session facts. Preparation: basic table reading, source totals and the retained prerequisite screen in COURSE.md. Suggested work: about five hours including practice and revision; unpiloted, not a minimum attendance requirement.

1. A decision is not yet a database question

A fictional neighbourhood workshop coordinator asks, “Are our workshops being used?” That could mean seats booked, seats attended, distinct people reached, or capacity filled. Each is a different question. SQL can calculate the wrong one perfectly.

For this course we ask two narrower questions: “How many booked seats are represented by these records?” and “What do the recorded attendance counts establish, given missing attendance?” The first population is the set of supplied booking records. The second needs an explicit missingness rule. We will not infer unique people, satisfaction, causes, or future demand. The data are invented for learning, not a report on a real organization.

A good question sentence names entity, time scope, measure, inclusion rule and intended use. Example: “For open sessions listed in the September synthetic fixture, compare booked seats with listed capacity to identify records worth checking before planning the next round.” This does not promise that a high booking ratio proves a session is popular or effective. It does tell us which records and denominator we need.

Write that sentence before SQL. If you cannot say whether a cancelled session belongs in a denominator, the obstacle is an unresolved definition, not missing syntax.

2. One row means one thing

The sessions table has one row per scheduled session. Session 101 is Repair at North on September 3, capacity 6, status open. The registrations table has one row per booking record, not one row per person or seat. Booking 1001 reserves two seats in session 101; booking 1002 reserves one. Therefore ten booking rows can represent fourteen booked seats.

A booking_ref is an invented booking contact reference. L01 occurs in more than one booking. It does not identify every person attending: a two-seat booking has only one reference. Even counting distinct references cannot establish unique people reached. A database column can be a perfectly good identifier for one purpose and inadequate for another.

The grain is the entity described by one row. A measure's grain matters too. capacity=6 belongs to session 101. seats=2 belongs to booking 1001. If the session row is repeated beside three bookings, the capacity does not become eighteen. You will investigate this failure directly in Unit 5.

Two-table grain and keys

Complete text alternative: a sessions row has primary key session_id; a registrations row has primary key reg_id and foreign key session_id. One session can have zero, one or many registrations. Each constrained registration refers to exactly one existing session. Capacity is stored once per session; booked and attended seat counts are stored once per registration. The arrow represents the relationship, not data movement.

3. Identity, candidate keys and repeated facts

A primary key is the chosen unique identifier for table rows. session_id and reg_id serve that role. A candidate key is any minimal set of fields guaranteed by the domain rules to identify a row uniquely. Looking unique in this tiny sample is not enough. Dates repeat in real schedules; two sessions may share a site, topic and date; names may change. The schema does not declare those combinations to be candidate keys.

A foreign key connects a child row's identifier to a parent key. registrations.session_id may repeat because several bookings can refer to one session. Repetition of a foreign key is not a duplicate-record error. Repetition of the same reg_id in a supposedly one-row-per-booking table is an identity conflict.

Imagine a flat sheet containing reg_id, session_id, topic, site, capacity, seats. If one session has three bookings, its topic, site and capacity appear three times. A correction to capacity could update only two rows, leaving incompatible versions of one fact. Moving session facts into sessions makes the intended dependency visible: session_id determines the session attributes. Booking facts remain in registrations, where reg_id determines them. This is a small normalization example, not a full treatment of functional dependencies or normal forms.

“Remove every repeated value” is not normalization. Two sessions can legitimately have capacity 4; two registrations can legitimately cost 600 cents. The aim is to store each fact at its proper entity grain, not to erase equal values.

4. Read the contract before loading

Open data/schema.sql. The teaching schema requires integer seat counts; seats must be positive; attendance may be unknown but, when recorded, is between zero and booked seats; fees cannot be negative. STRICT tables reject unsuitable types for these fields. PRAGMA foreign_keys=ON enables relationship enforcement on the connection. These rules catch some impossible records, not every false record. A made-up but internally valid attendance count could still pass.

starts_on is text in consistently padded YYYY-MM-DD form. That makes the supplied dates sortable as text. The schema does not validate every possible calendar date. A real intake would need an authoritative date-validation rule. booking_ref is required but not unique. The schema also does not impose cross-table capacity limits: a session could have more booked seats than capacity, which should trigger investigation rather than be assumed impossible.

The intake table is deliberately less constrained. It represents raw rows awaiting investigation. It is not silently added to the trusted registration population. Preserve raw evidence; do not “fix” it by inventing missing parents.

5. Trace a control before running anything

Read the ten base registration rows in DATA-DICTIONARY.md. Add seats: 2+1+1+2+2+1+2+1+1+1 = 14. Count rows: 10. Distinct booking references are 8, but they are not eight unique attendees. These are three different measures with different meanings.

Now run python3 run.py base --query u01_inventory. The result should have columns booking_rows, booking_refs, booked_seats and one row 10,8,14. Save the result, fixture name and runtime label. Execution checks that this SQL produced this result on this data. Your earlier arithmetic is a separate control; matching it is useful evidence, but it does not prove the question was suitable.

Practice and checkpoint

Complete G1 then I1 in WORKBOOK.md before opening the key. G1 uses the worked base fixture; I1 uses different October sessions. Draw the relationship or write its text equivalent. Explain which repetitions are expected and which need investigation. Checkpoint C1 asks you to reject the claim “ten registrations means ten people attended.”

Do not proceed merely because your row count matches. You should be able to explain why a session with no registrations still exists and why capacity cannot be summed across repeated booking rows. If those explanations are unclear, annotate the two tables by hand and retry the checkpoint. Your final project begins now: save a question-and-grain.md note with the question, population, table grains, keys and three claims the data cannot support.