Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — What INNER JOIN keeps
Which rows does an INNER JOIN return?
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.
FROM trainers tINNER JOIN codemon cON c.trainer_id = t.trainer_idWHERE c.level > 10Exercise 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';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;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?
ON c.trainer_id = t.trainer_idON c.codemon_id = t.trainer_idON c.name = t.nameON c.level = t.badgesHint : 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.
WHERE t.hometown = 'Stone Ridge'WHERE c.type = 'plant'WHERE t.name = 'Jordan'7310Exercise 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;781232Hint : 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?
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.
trainers.trainer_idcodemon.codemon_idcodemon.trainer_idcodemon.trainer_id IS NULLExercise 14 — ON or WHERE?
Where does the condition that matches the rows of the two tables belong?
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;AlexMorganTaylorJordanHint : 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.
ORDER BY c.level DESCORDER BY c.level ASCORDER BY c.capture_date ASCORDER BY t.name DESC, c.level DESCAquachuFirochuPyrochuRockachuExercise 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?
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.
VoltachuShadowchuRockachuPlantachuAlexMorganTaylorExercise 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;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?
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?
3478Hint : One of the four trainers owns nothing.