From a049d59fe90bbdd64d1743f85f121d574860766b Mon Sep 17 00:00:00 2001 From: Charlotte Beale Date: Sat, 21 Dec 2019 16:14:49 -0500 Subject: [PATCH] Complete --- .idea/.gitignore | 4 +++ .idea/DataGrip.PokemonSqlLab.iml | 9 ++++++ .idea/dataSources.local.xml | 22 +++++++++++++ .idea/dataSources.xml | 11 +++++++ .idea/misc.xml | 6 ++++ .idea/modules.xml | 8 +++++ .idea/vcs.xml | 6 ++++ Part 2.sql | 25 +++++++++++++++ Part3.sql | 55 ++++++++++++++++++++++++++++++++ Part4.sql | 21 ++++++++++++ 10 files changed, 167 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/DataGrip.PokemonSqlLab.iml create mode 100644 .idea/dataSources.local.xml create mode 100644 .idea/dataSources.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 Part 2.sql create mode 100644 Part3.sql create mode 100644 Part4.sql diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..64c8aaf --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,4 @@ +# Default ignored files +/workspace.xml +# Datasource local storage ignored files +/dataSources/ diff --git a/.idea/DataGrip.PokemonSqlLab.iml b/.idea/DataGrip.PokemonSqlLab.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/DataGrip.PokemonSqlLab.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/dataSources.local.xml b/.idea/dataSources.local.xml new file mode 100644 index 0000000..a6db44a --- /dev/null +++ b/.idea/dataSources.local.xml @@ -0,0 +1,22 @@ + + + + + + #@ + ` + + + master_key + root + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..14231b3 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,11 @@ + + + + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://localhost:3306 + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..28a804d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..1095880 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Part 2.sql b/Part 2.sql new file mode 100644 index 0000000..c964fd2 --- /dev/null +++ b/Part 2.sql @@ -0,0 +1,25 @@ +# Question: What are all the types of pokemon that a pokemon can have? +# Answer: Normal, Water, Grass, Rock, Fire, Ground, Poison, Bug, Electric, Dragon, Steel, Dark, Fighting, Psychic, Ghost, Fairy, Ice, Flying +SELECT GROUP_CONCAT(' ', name) +FROM types; + +# Question: What is the name of the pokemon with id 45? +# Answer: Eevee +SELECT name +FROM pokemons +WHERE id = 45; + +# Question: How many pokemon are there? +# Answer: 656 +SELECT COUNT(1) +FROM pokemons; + +# Question: How many types are there? +# Answer: 18 +SELECT COUNT(1) +FROM types; + +# Question: How many pokemon have a secondary type? +# Answer: 316 +SELECT COUNT(secondary_type) +FROM pokemons; diff --git a/Part3.sql b/Part3.sql new file mode 100644 index 0000000..acda058 --- /dev/null +++ b/Part3.sql @@ -0,0 +1,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 diff --git a/Part4.sql b/Part4.sql new file mode 100644 index 0000000..88387f7 --- /dev/null +++ b/Part4.sql @@ -0,0 +1,21 @@ +# Part 4: Final Report +# Explanation: Since pokelevel directly influences the fields HP, max HP, attack, defense, etc., + # I thought the best way to order the trainers would be take an average of the totals of these fields + # for each of their pokemon. The "best" trainer will have pokemon with the highest average in these fields. + +SELECT DISTINCT GROUP_CONCAT(p.name) as "Pokemon", tr.trainername as "Trainer", + GROUP_CONCAT(pt.pokelevel) as "PokeLevels", GROUP_CONCAT(t.name) as "Primary Types", + GROUP_CONCAT(r.name) as "Secondary Types", AVG_POINTS.total +FROM pokemon_trainer pt +JOIN ( + SELECT trainerID, AVG(hp + maxhp + attack + defense + spatk + spdef + speed) as total + FROM pokemon_trainer + GROUP BY trainerID +) AS AVG_POINTS ON AVG_POINTS.trainerID = pt.trainerID +JOIN pokemons p ON p.id = pt.pokemon_id +JOIN trainers tr ON tr.trainerID = pt.trainerID +JOIN types t ON t.id = p.primary_type +JOIN types r ON r.id = p.secondary_type +GROUP BY AVG_POINTS.total, tr.trainerID, tr.trainername +ORDER BY AVG_POINTS.total DESC +LIMIT 20;