A sql lab filled with pokemon data

PART3 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #*****************************************
  2. ##PART 3: JOINS AND GROUPS###
  3. #What is each pokemon's primary type?
  4. SELECT DISTINCT pokemons.name, types.name
  5. FROM pokemon.pokemons
  6. JOIN pokemon.types ON types.id = primary_type;
  7. #500
  8. #What is Rufflet's secondary type?
  9. SELECT DISTINCT pokemons.name, types.name
  10. FROM pokemon.pokemons
  11. JOIN pokemon.types ON types.id = secondary_type
  12. WHERE pokemons.name LIKE 'RUFFLET';
  13. #Flying
  14. #What are the names of the pokemon that belong to the trainer with trainerID 303?
  15. SELECT DISTINCT pokemons.name, pokemon.pokemon_trainer.trainerID
  16. FROM pokemon.pokemons
  17. JOIN pokemon.pokemon_trainer on trainerID
  18. WHERE trainerID LIKE 303;
  19. #500
  20. #How many pokemon have a secondary type Poison
  21. SELECT COUNT(pokemons.secondary_type)
  22. FROM pokemon.pokemons
  23. JOIN pokemon.types on types.id = secondary_type
  24. WHERE types.name LIKE 'Poison';
  25. #31
  26. #What are all the primary types and how many pokemon have that type?
  27. SELECT pokemon.types.name, COUNT(pokemons.primary_type)
  28. FROM pokemon.pokemons
  29. JOIN pokemon.types ON pokemons.primary_type = types.id
  30. GROUP BY types.id;
  31. #Normal 90
  32. #Water 95
  33. #Grass 59
  34. #Rock 38
  35. #Fire 38
  36. #Ground 29
  37. #Poison 27
  38. #Bug 61
  39. #Electric 35
  40. #Dragon 22
  41. #Steel 19
  42. #Dark 24
  43. #Fighting 25
  44. #Psychic 38
  45. #Ghost 19
  46. #Fairy 14
  47. #Ice 22
  48. #Flying 1
  49. #How many pokemon at level 100 does each trainer with at least one level 100 pokemone have?
  50. # (Hint: your query should not display a trainer
  51. SELECT pokemon_trainer.trainerID, COUNT(pokemon_trainer.pokelevel)
  52. FROM pokemon.pokemon_trainer
  53. WHERE pokelevel LIKE 100
  54. GROUP BY trainerID;
  55. #194
  56. #How many pokemon only belong to one trainer and no other?
  57. SELECT COUNT(*) AS "Legendaires"
  58. FROM (SELECT DISTINCT pokemon_id, COUNT(pokemon_id)
  59. FROM pokemon_trainer
  60. GROUP BY pokemon_id
  61. HAVING COUNT(DISTINCT trainerID) = 1) alias;
  62. #14