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