A sql lab filled with pokemon data

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. ## PART 3: JOINS AND GROUPS
  2. # What is each pokemons primary type?
  3. SELECT pokemons.name as "Species", types.name as "Type"
  4. FROM pokemons
  5. JOIN types ON (types.id = primary_type);
  6. # What is Rufflets secondary type?
  7. SELECT pokemons.name as "Species", types.name as "SecondType"
  8. FROM pokemons
  9. JOIN types ON (types.id = secondary_type)
  10. WHERE pokemons.name = "Rufflet";
  11. # What are the names of the pokemon that belong to the trainer with trainerID 303?
  12. SELECT trainers.trainername as "TrainerName", pokemons.name as "Species"
  13. FROM pokemon_trainer
  14. JOIN pokemons ON (pokemons.id = pokemon_id)
  15. JOIN trainers ON (trainers.trainerID = pokemon_trainer.trainerID)
  16. WHERE pokemon_trainer.trainerID = 303;
  17. # How many pokemon have a secondary type Poison?
  18. SELECT COUNT(types.id) as "NumOfSpecies", types.name as "SecondType"
  19. FROM pokemons
  20. JOIN types ON (types.id = secondary_type)
  21. WHERE types.name = "Poison";
  22. # What are all the primary types and how many pokemon have that type?
  23. SELECT types.name as "PrimeType", COUNT(pokemons.primary_type) as "NumOfSpecies"
  24. FROM pokemons
  25. JOIN types ON (types.id = primary_type)
  26. GROUP BY types.name;
  27. # 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)
  28. SELECT COUNT(pokelevel) as "NumOfLvl100"
  29. FROM pokemon_trainer
  30. WHERE pokelevel = 100
  31. GROUP BY trainerID;
  32. # How many pokemon only belong to one trainer and no other?
  33. SELECT COUNT(*) as "UniquelyOwned"
  34. FROM (SELECT DISTINCT pokemon_id, COUNT(pokemon_id)
  35. FROM pokemon_trainer
  36. GROUP BY pokemon_id
  37. HAVING COUNT(DISTINCT trainerID) = 1) alias;