A sql lab filled with pokemon data

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. USE pokemon;
  2. -- Part 2
  3. -- What are all the types of pokemon that a pokemon can have?
  4. SELECT name
  5. FROM types;
  6. -- What is the name of the pokemon with id 45?
  7. SELECT name
  8. FROM pokemons
  9. WHERE id = 45;
  10. -- How many pokemon are there?
  11. SELECT COUNT(id)
  12. FROM pokemons;
  13. -- How many types are there?
  14. SELECT COUNT(id)
  15. FROM types;
  16. -- How many pokemon have a secondary type?
  17. SELECT COUNT(id)
  18. FROM pokemons
  19. WHERE secondary_type IS NOT null;
  20. -- Part 3
  21. -- What is each pokemon's primary type?
  22. SELECT pokemons.name AS "pokemon species", types.name AS "type"
  23. FROM pokemons
  24. JOIN types ON (types.id = primary_type);
  25. -- What is Rufflet's secondary type?
  26. SELECT pokemons.name AS "Pokemon", types.name AS "Secondary Type"
  27. FROM pokemons
  28. JOIN types ON (types.id = secondary_type)
  29. WHERE pokemons.name = "Rufflet";
  30. -- What are the names of the pokemon that belong to the trainer with trainerID 303?
  31. SELECT trainers.trainername AS "Trainer Name", pokemons.name AS "Pokemon"
  32. FROM pokemon_trainer
  33. JOIN pokemons ON (pokemons.id = pokemon_id)
  34. JOIN trainers ON (trainers.trainerID = pokemon_trainer.trainerID) WHERE pokemon_trainer.trainerID = 303;
  35. -- How many pokemon have a secondary type Poison?
  36. SELECT COUNT(types.id) as "Number of Pokemon", types.name as "Secondary Type"
  37. FROM pokemons
  38. JOIN types ON (types.id = secondary_type)
  39. WHERE types.name = "Poison";
  40. -- What are all the primary types and how many pokemon have that type?
  41. SELECT types.name as "Primary Type", COUNT(pokemons.primary_type) as "Pokemon Count"
  42. FROM pokemons
  43. JOIN types ON (types.id = primary_type)
  44. GROUP BY types.name;
  45. -- How many pokemon at level 100 does each trainer with at least one level 100 pokemone have? (Hint: your query should not display a trainer)
  46. SELECT COUNT(pokelevel) as "Number of Level l100"
  47. FROM pokemon_trainer
  48. WHERE pokelevel = 100
  49. GROUP BY trainerID;
  50. -- How many pokemon only belong to one trainer and no other?
  51. SELECT COUNT(*) as "Unique Pokemon"
  52. FROM (SELECT DISTINCT pokemon_id, COUNT(pokemon_id)
  53. FROM pokemon_trainer
  54. GROUP BY pokemon_id
  55. HAVING COUNT(DISTINCT trainerID) = 1) alias;
  56. -- PART 4
  57. -- Arranged by highest average stats with slightly more weight in health and speed since you either need to hit first or withstand a hit in order to inflict damage to the enemy pokemon
  58. SELECT pokemons.name AS "Pokemon Name", trainers.trainername AS "Trainer Name", pokemon_trainer.pokelevel as "Level", ANY_VALUE(1st.name) as "Primary Type", ANY_VALUE(2nd.name) as "Secondary Type"
  59. FROM pokemon_trainer
  60. JOIN trainers ON (trainers.trainerID = pokemon_trainer.trainerID)
  61. JOIN pokemons ON (pokemons.id = pokemon_id)
  62. JOIN types 1st ON (1st.id = pokemons.primary_type)
  63. JOIN types 2nd ON (2nd.id = pokemons.secondary_type)
  64. GROUP BY pokemons.name, trainers.trainerID, pokelevel
  65. ORDER BY pokelevel DESC, SUM((pokemon_trainer.maxhp*1.5) + pokemon_trainer.attack + pokemon_trainer.defense + pokemon_trainer.spatk + pokemon_trainer.spdef + (pokemon_trainer.speed*1.5) / 6) DESC, pokemons.name DESC;