Quizz

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — What INNER JOIN keeps

Which rows does an INNER JOIN return?

Options
• Only the combinations where the ON condition is true
• All rows of the first table, matched or not
• All rows of both tables
• Only the rows that have a NULL foreign key

Hint : Think about Plantachu and Jordan.

Exercise 2 — The two keywords

Complete the query that lists each trainer with their Codémons.

Hint : Attach the table, then say how the rows match.

Exercise 3 — Anatomy of a join

Match each part of the query with its role.

Items to match
• FROM trainers t
• INNER JOIN codemon c
• ON c.trainer_id = t.trainer_id
• WHERE c.level > 10
Definitions
• The first table, with its alias
• The table attached to the first one
• The matching condition
• A filter applied to the joined rows

Exercise 4 — Taylor's team

What does this query return?

SELECT c.name FROM codemon c INNER JOIN trainers t ON c.trainer_id = t.trainer_id WHERE t.name = 'Taylor';
Options
• Florachu, Rockachu, Firochu
• Aquachu, Voltachu
• Taylor
• Every Codémon, because the WHERE is on the trainers table

Hint : Taylor is trainer number 3.

Exercise 5 — Ambiguous

Why does this query fail?

SELECT name FROM trainers t INNER JOIN codemon c ON c.trainer_id = t.trainer_id;
Options
• Both tables have a name column, so SQL doesn't know which one to show
• A join cannot select a single column
• The aliases t and c are too short
• trainer_id does not exist in codemon

Hint : The error message says "ambiguous column name".

Exercise 6 — Say which name

Complete the query so that the first column is the trainer's name and the second the Codémon's name.

Hint : Once a table has an alias, only the alias works in front of its columns.

Exercise 7 — The right condition

Which ON condition correctly joins codemon c to trainers t?

Options
• ON c.trainer_id = t.trainer_id
• ON c.codemon_id = t.trainer_id
• ON c.name = t.name
• ON c.level = t.badges

Hint : Join the foreign key to the primary key it points to.

Exercise 8 — How many rows?

Each query joins trainers t and codemon c on c.trainer_id = t.trainer_id. Match each WHERE with the number of rows returned.

Items to match
• No WHERE at all
• WHERE t.hometown = 'Stone Ridge'
• WHERE c.type = 'plant'
• WHERE t.name = 'Jordan'
Definitions
• 7
• 3
• 1
• 0

Exercise 9 — Count per trainer

Complete the query so that it returns the number of Codémons owned by each trainer.

Hint : One function counts rows, one clause makes one group per trainer.

Exercise 10 — Row count

There are 4 trainers and 8 Codémons, one of them wild. How many rows does this query return?

SELECT t.name, c.name FROM trainers t INNER JOIN codemon c ON c.trainer_id = t.trainer_id;
Options
• 7
• 8
• 12
• 32

Hint : One row per Codémon that has a trainer.

Exercise 11 — The missing Plantachu

Why does Plantachu never appear in an INNER JOIN between codemon and trainers?

Options
• Its trainer_id is NULL, and NULL matches no trainer
• Plant Codémons are excluded from joins
• Its level is too low
• It appears, with Jordan as trainer

Hint : Look at Plantachu's foreign key.

Exercise 12 — Filter on the other table

Complete the query so that it returns the Codémons of the trainers who live in Azure City, in alphabetical order of Codémon name.

Hint : The city is a trainer column; the sort key is a Codémon column.

Exercise 13 — Keys in our database

Match each column with what it is.

Items to match
• trainers.trainer_id
• codemon.codemon_id
• codemon.trainer_id
• codemon.trainer_id IS NULL
Definitions
• The primary key of trainers
• The primary key of codemon
• A foreign key pointing to trainers
• A wild Codémon with no owner

Exercise 14 — ON or WHERE?

Where does the condition that matches the rows of the two tables belong?

Options
• In the ON clause of the join
• In the SELECT list
• In GROUP BY
• In ORDER BY

Hint : Look at the syntax template of chapter 8.2: which keyword comes right after the second table?

Exercise 15 — Average level per trainer

Complete the query so that it returns the average level of each trainer's Codémons.

Hint : Match the foreign key with the primary key.

Exercise 16 — Strongest partner

Which trainer's name does this query return?

SELECT t.name FROM trainers t INNER JOIN codemon c ON c.trainer_id = t.trainer_id ORDER BY c.level DESC LIMIT 1;
Options
• Alex
• Morgan
• Taylor
• Jordan

Hint : Which Codémon has the highest level, and who owns it?

Exercise 17 — Queries and their first row

Each query joins trainers t and codemon c and returns c.name. Match each ORDER BY with the first Codémon returned.

Items to match
• ORDER BY c.level DESC
• ORDER BY c.level ASC
• ORDER BY c.capture_date ASC
• ORDER BY t.name DESC, c.level DESC
Definitions
• Aquachu
• Firochu
• Pyrochu
• Rockachu

Exercise 18 — Above level 12

Complete the query so that it returns the Codémons strictly above level 12 with their trainer, highest level first.

Hint : Strictly above excludes 12 itself; sort on the column you are filtering.

Exercise 19 — Count the team

Which query returns, for each trainer who owns at least one Codémon, how many Codémons they own?

Options
• SELECT t.name, COUNT(*) FROM trainers t INNER JOIN codemon c ON c.trainer_id = t.trainer_id GROUP BY t.name;
• SELECT t.name, COUNT(*) FROM trainers t INNER JOIN codemon c ON c.trainer_id = t.trainer_id GROUP BY c.name;
• SELECT t.name, COUNT(*) FROM trainers t INNER JOIN codemon c ON c.trainer_id = t.trainer_id;
• SELECT t.name, COUNT(*) FROM trainers t GROUP BY t.name;

Hint : One group per trainer, and the Codémons must be joined to be counted.

Exercise 20 — Who owns what

Match each Codémon with its trainer.

Items to match
• Voltachu
• Shadowchu
• Rockachu
• Plantachu
Definitions
• Alex
• Morgan
• Taylor
• Nobody: its trainer_id is NULL

Exercise 21 — Choose the aliases

Complete the aliases so that the query works: t must stand for trainers and c for codemon.

Hint : The alias is written right after the table name.

Exercise 22 — The forgotten ON

What is wrong with this query?

SELECT t.name, c.name FROM trainers t INNER JOIN codemon c;
Options
• There is no ON: every trainer gets paired with every Codémon (4 × 8 = 32 rows)
• It is missing a GROUP BY clause
• Aliases cannot be a single letter
• Nothing: it returns the 7 real pairs

Hint : Without a matching condition, SQL has no way to know which rows go together.

Exercise 23 — Recent captures

Complete the query so that it returns the Codémons captured on 5 February 2023 or later, with their trainer, most recent first.

Hint : "Or later" includes the date itself.

Exercise 24 — The link

Which pair of columns links the two tables?

Options
• codemon.trainer_id and trainers.trainer_id
• codemon.name and trainers.name
• codemon.codemon_id and trainers.trainer_id
• codemon.level and trainers.badges

Hint : A foreign key and the primary key it points to.

Exercise 25 — How many trainers?

How many different trainers appear in the result of an INNER JOIN between trainers and codemon?

Options
• 3
• 4
• 7
• 8

Hint : One of the four trainers owns nothing.

Back to the course