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

Preview

DA 2.6: Joining tables to meet a business need

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

The librarian asks for a report

A librarian wants a list of every member, with how many books each has out. Members with none must still show.

All the data is already in one library system. The job is to choose the right join.

Key words

WordWhat it means
Business requirementwhat the report must show, in the customer's words
Combined datasetone result made by joining tables
Single sourceall the tables live in one database
Left joinkeeps every row of the left table
Inner joinkeeps only the rows that match in both tables
Databasean organised store of data that you can search

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

By the end of this lesson

Note

I can choose and apply a join so the combined dataset meets a requirement.

Starter

Have a go

Starter

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.

Assess the requirement first

Read the requirement and pull out three things: what rows must appear, which columns, and what happens to rows with no match.

That last point chooses the join. "Members with none must still show" means a left join from members.

From requirement to join

Requirement saysJoin to use
Only members who have borrowedInner join
Every member, even with no loansLeft join from members
Every book, even never borrowedLeft join from books
Every member and every book, matched or notFull join

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

Try it: every member and their loans out

Code: duckdb
select members.name, count(loans.loan_id) as loans_out
from members
left join loans on loans.member_id = members.member_id
  and loans.returned is null
group by members.name
order by members.name;

-- Ada and Mia show with 0. An inner join would hide them.

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

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.