瀏覽代碼

completed lab

Christian Sheridan 6 年之前
父節點
當前提交
e8a6075226
共有 3 個檔案被更改,包括 80 行新增0 行删除
  1. 19
    0
      Part 2
  2. 45
    0
      Part 3
  3. 16
    0
      Part 4

+ 19
- 0
Part 2 查看文件

@@ -0,0 +1,19 @@
1
+CREATE SCHEMA pokemon;
2
+
3
+
4
+## PART 2 ##
5
+#what are all the types of pokemon that a pokemon can have?
6
+SELECT name FROM pokemon.types;
7
+
8
+#What is the name of the pokemon with id 45?
9
+SELECT name FROM pokemon.pokemons WHERE id = 45;
10
+
11
+#How many pokemon are there?
12
+SELECT COUNT(*) FROM pokemon.pokemons;
13
+
14
+#How many types are there?
15
+SELECT COUNT(*) FROM pokemon.types;
16
+
17
+#How many pokemon have a secondary type?
18
+SELECT COUNT(secondary_type) FROM pokemon.pokemons;
19
+CREATE SCHEMA pokemon;

+ 45
- 0
Part 3 查看文件

@@ -0,0 +1,45 @@
1
+### PART 3 ###
2
+#each pokemons primary type
3
+SELECT p.name, pt.name
4
+FROM pokemon.pokemons p
5
+JOIN pokemon.types pt
6
+ON pt.id = p.primary_type;
7
+
8
+#What is Rufflet's secondary type
9
+SELECT p.name, t.name
10
+FROM pokemon.pokemons p
11
+JOIN pokemon.types t
12
+ON p.secondary_type = t.id WHERE p.name = "rufflet";
13
+
14
+#what are the names of the pokemon that belong to the trainer with the trainerID 303?
15
+SELECT p.name
16
+FROM pokemon.pokemons p
17
+JOIN pokemon.pokemon_trainer ptrain
18
+ON ptrain.pokemon_id = p.id WHERE ptrain.trainerID = 303;
19
+
20
+#How many pokemon have a secondary type Poison
21
+SELECT count(p.secondary_type)
22
+FROM pokemon.pokemons p
23
+JOIN pokemon.types pt
24
+ON p.secondary_type = pt.id WHERE pt.name = "poison";
25
+
26
+#What are all the primary types and how many pokemon have that type?
27
+SELECT pt.name , count(pt.id) AS "Number of pokemon"
28
+FROM pokemon.pokemons p
29
+JOIN pokemon.types pt
30
+ON p.primary_type = pt.id
31
+GROUP BY pt.name;
32
+
33
+#how many pokemon at level 100 does each trainer with at least one level 100 pokemon have?
34
+#hint: your query should not display a trainer
35
+SELECT count(ptrain.pokelevel) AS "number of pokemon above 100 for each trainer with at least one pokemon above 100"
36
+FROM pokemon.pokemon_trainer ptrain
37
+WHERE ptrain.pokelevel = 100
38
+GROUP BY ptrain.trainerID;
39
+
40
+#how many pokemon only belong to one trainer and no other?
41
+SELECT COUNT(*) as "UniquelyOwned"
42
+FROM (SELECT DISTINCT pokemon_id, COUNT(pokemon_id)
43
+      FROM pokemon.pokemon_trainer
44
+      GROUP BY pokemon_id
45
+      HAVING COUNT(DISTINCT trainerID) = 1) alias;

+ 16
- 0
Part 4 查看文件

@@ -0,0 +1,16 @@
1
+
2
+#### PART 4 ####
3
+#Write a query that returns the following columns: Pokemon Name, Trainer Name, Level
4
+#                                                     Primary Type, Secondary Type
5
+#it ranks the trainers based on their pokemons level
6
+
7
+
8
+SELECT poke.name as "Pokemon Name", pt.trainername as "Trainer Name", ptrain.pokelevel as "Level",
9
+       ptypes.name as "Primary Type", ptypes2.name as "Secondary Type"
10
+FROM pokemon.pokemons poke
11
+JOIN pokemon.pokemon_trainer ptrain ON ptrain.pokemon_id = poke.id
12
+JOIN pokemon.types ptypes ON poke.primary_type = ptypes.id
13
+join pokemon.types ptypes2 ON poke.secondary_type = ptypes2.id
14
+join pokemon.trainers pt ON ptrain.trainerID = pt.trainerID
15
+ORDER BY ptrain.pokelevel;
16
+