|
@@ -0,0 +1,48 @@
|
|
1
|
+#Part 3: Joins and Groups
|
|
2
|
+
|
|
3
|
+## What is each pokemon's primary type?
|
|
4
|
+SELECT pok.name, t.name FROM pokemon.pokemons pok
|
|
5
|
+JOIN pokemon.types t ON pok.primary_type = t.id;
|
|
6
|
+
|
|
7
|
+## What is Rufflet's secondary type?
|
|
8
|
+SELECT pok.name, t.name FROM pokemon.pokemons pok
|
|
9
|
+JOIN pokemon.types t
|
|
10
|
+ ON pok.secondary_type = t.id
|
|
11
|
+WHERE pok.name = "Rufflet";
|
|
12
|
+
|
|
13
|
+## What are the names of the pokemon that belong to the trainer with trainerID 303?
|
|
14
|
+SELECT trainer.trainername AS "Trainer", GROUP_CONCAT(pok.name) AS "Pokemons"
|
|
15
|
+FROM pokemon.pokemons pok
|
|
16
|
+JOIN pokemon.pokemon_trainer pokTrainer
|
|
17
|
+ ON pokTrainer.pokemon_id = pok.id
|
|
18
|
+JOIN pokemon.trainers trainer
|
|
19
|
+ ON pokTrainer.trainerID = trainer.trainerID
|
|
20
|
+WHERE trainer.trainerID = 303
|
|
21
|
+GROUP BY trainer.trainername;
|
|
22
|
+
|
|
23
|
+## How many pokemon have a secondary type Poison
|
|
24
|
+SELECT COUNT(pok.secondary_type) AS "Poison Pokemons"
|
|
25
|
+FROM pokemon.pokemons pok
|
|
26
|
+JOIN pokemon.types types
|
|
27
|
+ ON pok.secondary_type = types.id
|
|
28
|
+WHERE types.name = "Poison";
|
|
29
|
+
|
|
30
|
+## What are all the primary types and how many pokemon have that type?
|
|
31
|
+SELECT type.name AS "Type", COUNT(pok.name) AS "Number of Pokemon"
|
|
32
|
+FROM pokemon.types type
|
|
33
|
+JOIN pokemon.pokemons pok
|
|
34
|
+ ON pok.primary_type = type.id
|
|
35
|
+GROUP BY type.name;
|
|
36
|
+
|
|
37
|
+## How many pokemon at level 100 does each trainer with at least one level 100 pokemon have?
|
|
38
|
+## (Hint: your query should not display a trainer
|
|
39
|
+SELECT COUNT(trainer.pokemon_id) as "Pokemon at Level 100"
|
|
40
|
+FROM pokemon.pokemon_trainer trainer
|
|
41
|
+WHERE trainer.pokelevel = 100
|
|
42
|
+GROUP BY trainer.trainerID;
|
|
43
|
+
|
|
44
|
+## How many pokemon only belong to one trainer and no other?
|
|
45
|
+SELECT COUNT(pokemon_id) AS "Pokemon with only one trainer"
|
|
46
|
+FROM (SELECT DISTINCT pokemon_id, COUNT(pokemon_id)
|
|
47
|
+FROM pokemon.pokemon_trainer
|
|
48
|
+GROUP BY pokemon_id HAVING COUNT(DISTINCT trainerID) = 1)alias;
|