A sql lab filled with pokemon data

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #Part 3: Joins and Groups
  2. # What is each pokemon's primary type?
  3. SELECT pokemons.name, types.name
  4. FROM pokemons
  5. JOIN types
  6. ON pokemons.primary_type = types.id;
  7. # What is Rufflet's secondary type? --- couln't make it work without using the id :(
  8. SELECT pokemons.name, pokemons.secondary_type, types.name
  9. FROM pokemons
  10. JOIN types
  11. ON pokemons.secondary_type = types.id
  12. WHERE pokemons.id = 644;
  13. # What are the names of the pokemon that belong to the trainer with trainerID 303?
  14. SELECT pokemons.name
  15. FROM pokemons
  16. JOIN pokemon_trainer
  17. ON pokemons.id = pokemon_trainer.trainerID
  18. WHERE pokemon_trainer.trainerID = 303;
  19. # How many pokemon have a secondary type Poison?
  20. SELECT COUNT(pokemons.id)
  21. FROM pokemons
  22. JOIN types
  23. ON pokemons.secondary_type = types.id
  24. WHERE types.name = 'Poison';
  25. #What are all the primary types and how many pokemon have that type?
  26. SELECT types.name, COUNT(pokemons.primary_type)
  27. FROM types
  28. JOIN pokemons
  29. ON pokemons.primary_type = types.id
  30. GROUP BY types.name;
  31. # How many pokemon at lvl 100 does each trainer w/ at least one level 100 pokemone have?(query shouldn't display a trainer)
  32. SELECT COUNT(pokemon_id) #not sure if this is right because it seems incomplete w/o trainer name
  33. FROM pokemon_trainer
  34. WHERE pokelevel = 100
  35. GROUP BY trainerID
  36. HAVING COUNT(pokelevel) > 1;
  37. # How many pokemon only belong to one trainer and no other?
  38. SELECT COUNT(trainerID), pokemon_id
  39. FROM pokemon_trainer
  40. GROUP BY pokemon_id
  41. HAVING COUNT(*) = 1;