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

Preview

Unit 9 A2: Queries that make reports for readers

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

55 minutes

Three readers, three reports

The owner of Harbour Sports (a made-up shop) wants to know how much money came in. The stock manager wants to know what to reorder. The marketing helper wants to know where the customers live.

All the data is in one set of linked tables. Each person needs a different question asked of it.

By the end of this lesson

Note

I can write a query (a question you ask a table of data) that produces a report for a chosen reader.

Key words

WordWhat it meansExample
Stakeholdera person who needs or is affected by the report
Aggregate functiona function that adds up or counts many recordsSUM, COUNT
Joina link that brings fields from two tables together
Aliasa plain name you give a column in the reportAS Revenue
Calculated fielda column worked out from other columns

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

Starter: what you already know

Have a go

Starter: what you already know

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

Aggregates turn many rows into one answer

A report seldom shows every sale. It shows a total for each group. An aggregate function works on many records at once. COUNT counts them and SUM adds them. AVG finds the average. MAX and MIN find the biggest and smallest.

GROUP BY makes one answer for each group, such as each product.

Try it: units sold for each product

Code: sql
-- A join brings the product name into the sales.
SELECT products.product_name AS Product,
       SUM(sales.quantity) AS Units
FROM sales
JOIN products ON sales.product_id = products.product_id
GROUP BY products.product_id
ORDER BY Units DESC, Product;

-- Change SUM(sales.quantity) to COUNT(*) to count sales instead.

Check

Have a go

Check

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.

The lesson goes on

11 more parts of this lesson are in the full lesson, which colleges teach from the Library. A college account gives your students all of it.