A sql lab filled with pokemon data

pokemonQueries.sql 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. --What is the name of the pokemon with id 45?
  2. select p.name from pokemons p where p.id = 40
  3. --How many pokemon are there?
  4. select count(*) from pokemons
  5. --How many types are there?
  6. select count(*) from types as PokemonType
  7. --How many pokemon have a secondary type?
  8. select count(p.name) from pokemons p where p.secondary_type is not null
  9. --What is each pokemon's primary type?
  10. select p.name, t.name
  11. from pokemons p
  12. join types t
  13. on p.primary_type = t.id
  14. --What is Rufflet's secondary type?
  15. select p.name, t.name
  16. from pokemons p
  17. join types t
  18. on p.secondary_type = t.id
  19. where p.name = "Rufflet"
  20. ----What are the names of the pokemon that belong to the trainer with trainerID 303?
  21. select p.name
  22. from pokemon_trainer pt
  23. join pokemons p
  24. on pt.pokemon_id = p.id
  25. join trainers t
  26. on pt.trainerID = t.trainerID
  27. where pt.trainerID = 303
  28. --How many pokemon have a secondary type Poison
  29. select p.name, t.name
  30. from pokemons p
  31. join types t
  32. on p.secondary_type = t.id
  33. where t.name = "Poison"
  34. --What are all the primary types and how many pokemon have that type?
  35. select t.name, count(p.name)
  36. from pokemons p
  37. join types t
  38. on t.id = p.primary_type
  39. group by t.name
  40. --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
  41. create temporary table amount100
  42. select count(pt.pokelevel) as amount, trainerID
  43. from pokemon_trainer pt
  44. where pt.pokelevel = 100
  45. group by trainerID
  46. having amount > 1;
  47. select amount as "amount of level 100", count(trainerID) as "trainers with this amount"
  48. from amount100
  49. where amount > 0
  50. group by amount;
  51. --How many pokemon only belong to one trainer and no other?
  52. select
  53. p.name, count(distinct trainerID) as trainerAmt
  54. from
  55. pokemon_trainer pt
  56. join
  57. pokemons p
  58. on
  59. p.id = pt.pokemon_id
  60. group by
  61. p.name
  62. having
  63. count(distinct trainerID) = 1;
  64. /*
  65. Sort the data by finding out which trainer has the strongest
  66. pokemon so that this will act as a ranking of strongest to
  67. weakest trainer. You may interpret strongest in whatever way
  68. you want, but you will have to explain your decision.
  69. - I chose to sort the pokemon first by ghost type,
  70. I did this because ghost type is tied with dragon type for the
  71. least amount of vulnerabilities. Dragon is only vulnerable to steel
  72. as where ghost is only vulnerable to dark however there are more
  73. less dark type pokemon in the database then there are steel type
  74. indicating that dragon is more likely to face a challenging opponant.
  75. I then calculated the sum of all of its attributes and divided this
  76. by the amount of attributes total, effectively gather the AVG of its
  77. stats.
  78. */
  79. select count(ty.name),
  80. p.name,
  81. (sum(pt.attack + pt.spatk + pt.defense + pt.spdef + pt.hp + pt.maxhp + pt.speed)/7) as "AVGSTATS",
  82. pt.pokelevel,
  83. t.trainerID
  84. from pokemon_trainer pt
  85. join pokemons p
  86. on p.id = pt.pokemon_id
  87. join trainers t
  88. on t.trainerID = pt.trainerID
  89. join types ty
  90. on ty.id = p.primary_type
  91. join types ty2
  92. on ty2.id = p.secondary_type
  93. where ty.name = "Ghost"
  94. group by
  95. p.name,
  96. pt.pokelevel,
  97. t.trainerID
  98. order by AVGSTATS desc