Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

DA 3.7, 3.9: Testing a dataset against requirements

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

60 minutes

The total that was too good

The made-up shop owner reads the new report. "Sales are up!" she says. Then somebody notices one sale is dated next month and another has a price below zero.

A dataset (a set of data ready to analyse) that has never been tested is a guess. Today we write the tests first, then run them.

Key words

WordWhat it meansExample
SQLA language for asking questions of data held in tables.select count(*) from sales_dataset
RequirementWhat the finished data must be able to do or show.Every sale has a category
Test planA list of checks, with what a pass looks like for each.No repeated ref numbers
AssertionA statement that must be true. A check counts the rows where it is not.No price is below zero
DuplicateThe same thing recorded more than once.Sale T3 twice
Range checkA check that values sit between a lowest and a highest allowed value.Price from 0.50 to 50
AppropriateRight for the job: enough rows, the right type, and a usable form.A month of sales for a month report
ReviewLook back at the results and decide what to do next.Fix, retest or accept

The cells students fill in are left blank in this preview.

By the end of this lesson

Note

I can plan tests for a dataset, run them in SQL, read the results against the requirements and review the outcome.

Starter: last lesson

Have a go

Starter: last lesson

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Plan the tests from the requirements

Start with the requirements. Turn each one into a check that counts the rows that break it. Decide before you run what a pass is.

The shop owner has five requirements. A pass means zero rows break the rule. Writing the plan first stops you from bending the rule to fit the result.

Worked example: a test plan

RequirementThe checkA pass is
R1: one row per saleCount rows minus different ref numbers0 repeats
R2: every sale has a categoryCount rows with a null category0 nulls
R3: price from 0.50 to 50Count rows outside the range0 outside
R4: only September 2026Count rows with a date outside the month0 outside
R5: at least 5 rowsCount rows, then compare with 55 or more

The cells students fill in are left blank in this preview.

Try it: run the test plan

Code: duckdb
select 'R1 repeats' as requirement, count(*) - count(distinct ref) as failures from sales_dataset
union all select 'R2 no category', count(*) - count(category) from sales_dataset
union all select 'R3 price range', count(*) from sales_dataset where price < 0.50 or price > 50
union all select 'R4 September', count(*) from sales_dataset where sold_on < '2026-09-01' or sold_on > '2026-09-30'
union all select 'R5 enough rows', case when count(*) >= 5 then 0 else 1 end from sales_dataset;

Read the results against the plan

Every failures figure above 0 is a fail. Four requirements fail here. Only R5 passes.

For each fail, find the rows. Decide the cause: a repeat to remove, a gap in a lookup table, a bad price to check with the shop. Fix the cause, then run every test again. A fix can break another test.

The lesson goes on

6 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.