forked from kristofer/PokemonSqlLab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart3.sql
More file actions
55 lines (49 loc) · 1.34 KB
/
part3.sql
File metadata and controls
55 lines (49 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Question: What is each pokemon's primary type?
# Answer: Omitted for brevity
SELECT p.name, t.name
FROM pokemons p
JOIN types t
ON p.primary_type = t.id;
# Question: What is Rufflet's secondary type?
# Answer: Flying
SELECT t.name
FROM pokemons p
JOIN types t
ON p.secondary_type = t.id
WHERE p.name = 'Rufflet';
# Question: What are the names of the pokemon that belong to the trainer with trainerID 303?
# Answer: Wailord & Vileplume
SELECT p.name
FROM pokemon_trainer pt
JOIN pokemons p
ON pt.pokemon_id = p.id
WHERE pt.trainerID = 303;
# Question: How many pokemon have a secondary type Poison
# Answer: 31
SELECT COUNT(1)
FROM pokemons p
JOIN types t
ON p.secondary_type = t.id
WHERE t.name = 'Poison';
# Question: What are all the primary types and how many pokemon have that type?
# Answer: Omitted for brevity
SELECT t.name, COUNT(1)
FROM pokemons p
JOIN types t
ON p.primary_type = t.id
GROUP BY t.name;
# Question: How many pokemon at level 100 does each trainer with at least one level 100 pokemone have?
# Answer: Omitted for brevity
SELECT trainerID, COUNT(1)
FROM pokemon_trainer
WHERE pokelevel = 100
GROUP BY trainerID;
# Question: How many pokemon only belong to one trainer and no other?
# Answer: 13
SELECT COUNT(pokemon_id)
FROM (
SELECT pokemon_id
FROM pokemon_trainer
GROUP BY pokemon_id
HAVING COUNT(*) = 1
) AS ONE_OCCURRENCE