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

Preview

AC 2.1: Joining tables with INNER, LEFT and RIGHT

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 sale that names no product

The sales list says product 5 sold three times. A pupil does not know what product 5 is.

The name is in another table. To read both together, the computer has to match the two tables up.

Key words

WordWhat it means
Databasean organised store of tables that you can ask questions
SQLthe language used to ask a database for data
Queryone question written in SQL
Joinputs rows from two tables side by side where a key matches
Primary keythe column that gives every row in a table its own id
Foreign keya column that holds the primary key of a row in another table
ONthe part of a join that says which two columns must match
NULLa gap: the join found no matching row

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

By the end of this lesson

Note

I can write an inner join, a left join and a right join, and say which rows each one keeps.

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.

Why tables are joined

The products table gives each product a primary key, product_id. The sales table keeps that same number in its own product_id column, as a foreign key.

A join matches the foreign key to the primary key, so each sale can show the product name.

Try it: an inner join

Code: sql
SELECT sales.sale_id, products.name, sales.qty
FROM sales
INNER JOIN products
  ON sales.product_id = products.product_id
ORDER BY sales.sale_id;

-- Count the rows. Sale 11 is missing. Why?

INNER JOIN keeps matches only

An inner join keeps a row only when both sides match. Sale 11 points at product 99, which is not in the products table. So sale 11 is dropped. So are products that never sold.

Write table.column so SQL knows which table a column belongs to.

Three joins side by side

JoinKeepsGaps (NULL) appear forRows for the shop data
INNER JOINOnly rows that match on both sidesNothing11 (sale 11 is dropped)
LEFT JOINEvery row of the first table, with matchesRows of the first table with no match14 for products first
RIGHT JOINEvery row of the second table, with matchesRows of the second table with no match12 for sales second

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

Try it: a left join

Code: sql
SELECT products.name, sales.sale_id
FROM products
LEFT JOIN sales
  ON products.product_id = sales.product_id
ORDER BY products.name;

-- Which products have NULL for the sale?

The lesson goes on

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