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

Preview

DA 5.10: Aggregating data safely with SQL

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 group of one

A canteen report says: "Year 13 average rating for salad: 5." There is only one Year 13 student in the survey. Everyone now knows exactly what that student said.

Totals and averages hide people. But only if the groups are big enough.

Key words

WordWhat it means
Personal dataInformation about a living person who can be identified.
AggregationCombining many rows into one summary, such as a count or an average.
GroupA set of rows that share a value, such as the same year group.
Small numberA group so small that a total could point to one person.
ThresholdThe smallest group size you allow in a report. This lesson uses 3.
Primary useUsing data for the reason it was first collected.
Secondary useUsing data for a new reason after it was collected.
DatabaseAn organised store of data in tables.
SQLThe language used to ask a database questions.
QueryA question you ask a database.

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

By the end of this lesson

Note

I can aggregate data with SQL and hide groups that are too small, so a report cannot point to one person.

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.

Ways to aggregate

Aggregation turns many rows into a few numbers. Common methods are a count, a total, an average, a smallest and largest value, and a band such as "ages 15 to 16".

The report shows the summary. The single rows stay in the secure system.

Methods of aggregation in SQL

MethodSQL wordShows
CountCOUNT(*)How many rows in each group
TotalSUM(column)The rows added up
AverageAVG(column)The usual value
Smallest and largestMIN and MAXThe range
GroupGROUP BYOne summary line per group

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

Try it: count and average by year group

Code: sql
SELECT year_group, COUNT(*) AS students, AVG(rating) AS average_rating
FROM survey
GROUP BY year_group
ORDER BY year_group;

Hide the small groups

Look at the Year 13 line. It has a count of 1. A small group can point to one person.

Pick a minimum group size, a threshold. The ICO does not give one number. Your policy sets it. Here we use 3. SQL hides small groups with HAVING.

Try it: only groups of 3 or more

Code: sql
SELECT year_group, COUNT(*) AS students, AVG(rating) AS average_rating
FROM survey
GROUP BY year_group
HAVING COUNT(*) >= 3
ORDER BY year_group;

The lesson goes on

7 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.