A sql lab filled with pokemon data

MyPokemon.sql 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #*+*+*+*+*+*+*+*+*+**++*+*+*+*+*+*
  2. #Part 2: Simple Selects and Counts
  3. #*+*+*+*+*+*+*+*+*+**++*+*+*+*+*+*
  4. # What are all the types of pokemon that a pokemon can have?
  5. select distinct (name) from pokemon.types
  6. # What is the name of the pokemon with id 45?
  7. select name,id from pokemon.pokemons
  8. where id = 45
  9. #How many pokemon are there?
  10. select count(distinct(name)) as pokemon from pokemon.pokemons
  11. #How many types are there?
  12. select count(*) as "Num of Pokemon types"
  13. from pokemon.types
  14. #How many pokemon have a secondary type?
  15. select count(distinct(secondary_type)) as secondary_type from pokemon.pokemons
  16. #*+*+*+*+*+*+*+*+*+**++*+*
  17. #Part 3: Joins and Groups
  18. #*+*+*+*+*+*+*+*+*+**++*+*
  19. #What is each pokemon's primary type?
  20. SELECT x.name as pokemon_name, y.name as pokemon_type
  21. FROM pokemon.pokemons x LEFT JOIN pokemon.types y
  22. ON x.primary_type = y.id;
  23. #What is Rufflet's secondary type?
  24. SELECT x.name, y.name as secondary_type
  25. FROM pokemon.pokemons x LEFT JOIN pokemon.types y
  26. ON x.secondary_type = y.id
  27. WHERE x.name = 'Rufflet';
  28. #What are the names of the pokemon that belong to the trainer with trainerID 303?
  29. SELECT z.trainername as "Trainer", group_concat(x.name) as "Pokemons"
  30. FROM pokemon.pokemons x LEFT JOIN pokemon.pokemon_trainer y
  31. ON y.pokemon_id = x.id LEFT JOIN pokemon.trainers z
  32. ON y.trainerID = z.trainerID
  33. WHERE z.trainerID = 303
  34. GROUP BY z.trainername;
  35. #How many pokemon have a secondary type Poison?
  36. SELECT count(*) as secondary_type_Poison
  37. FROM pokemon.pokemons x LEFT JOIN pokemon.types y
  38. ON x.secondary_type = y.id
  39. WHERE y.name = 'Poison';
  40. #What are all the primary types and how many pokemon have that type?
  41. SELECT x.name as primary_types, count(y.name) as pokemon_count
  42. FROM pokemon.types x LEFT JOIN pokemon.pokemons y
  43. ON x.id = y.primary_type
  44. GROUP BY x.name;
  45. #How many pokemon at level 100 does each trainer with at least one level 100 pokemon have?
  46. select trainerid, count(pokelevel) as level_100_pokemon
  47. from pokemon.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(1) as pokemon_has_one_trainer
  52. FROM (select x.name, count(y.trainerID)
  53. from pokemon.pokemons x left join pokemon.pokemon_trainer y
  54. on x.id = y.pokemon_id
  55. group by x.name
  56. having count(y.trainerID) = 1) a;
  57. #*+*+*+*+*+*+*+*+*+**++*+*
  58. #Part 4: Final Report
  59. #*+*+*+*+*+*+*+*+*+**++*+*
  60. #Final Report [explain your decision - Pokemon with the highest level is the strongest pokemon and has strongest trainer and the bottom one has the weakest trainer]
  61. SELECT a.name as Pokemon_Name , b.trainername as Trainer_Name , c.PokeLevel, d.name as Primary_Type , e.name as Secondary_Type
  62. from pokemon.pokemon_trainer c join pokemon.pokemons a
  63. on a.id = c.pokemon_id join pokemon.trainers b
  64. on b.trainerID = c.trainerID join pokemon.types d
  65. on a.primary_type = d.id join pokemon.types e
  66. on a.secondary_type = e.id
  67. order by PokeLevel desc #Sort the data by finding out which trainer has the strongest pokemon so that this will act as a ranking of strongest to weakest trainer.