A sql lab filled with pokemon data

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*What is each pokemon's primary type?*/
  2. SELECT p.name, t.name FROM pokemon.pokemons p
  3. JOIN pokemon.types t
  4. ON p.primary_type = t.id;
  5. /*What is Rufflet's secondary type?*/
  6. SELECT p.name, t.name
  7. FROM pokemon.pokemons p
  8. JOIN pokemon.types t
  9. ON p.secondary_type = t.id
  10. WHERE p.name = 'Rufflet';
  11. /*What are the names of the pokemon that belong to the trainer with trainerID 303?*/
  12. SELECT p.name, tr.trainerID
  13. FROM pokemon.pokemons p
  14. JOIN pokemon.pokemon_trainer tr
  15. ON p.id = tr.pokemon_id
  16. WHERE tr.trainerID = 303;
  17. /*How many pokemon have a secondary type `Poison`*/
  18. SELECT COUNT(p.id) AS pokemon_count, t.name
  19. FROM pokemon.pokemons p
  20. JOIN pokemon.types t
  21. ON p.secondary_type = t.id
  22. WHERE t.name = 'Poison';
  23. /* What are all the primary types and how many pokemon have that type?*/
  24. SELECT t.name, COUNT(p.id)
  25. FROM pokemon.pokemons p
  26. JOIN pokemon.types t
  27. ON p.primary_type = t.id
  28. GROUP BY t.id;
  29. /*How many pokemon at level 100 does each trainer with at least one level 100 pokemone have?*/
  30. SELECT t.trainerID, COUNT(tr.pokelevel) AS pokemon_count
  31. FROM pokemon.pokemon_trainer tr
  32. JOIN pokemon.trainers t
  33. ON tr.trainerID = t.trainerID
  34. WHERE tr.pokelevel = 100
  35. GROUP BY tr.trainerID;
  36. /*How many pokemon only belong to one trainer and no other?*/
  37. SELECT DISTINCT pokemon_id, COUNT(pokemon_id)
  38. FROM pokemon_trainer
  39. GROUP BY pokemon_id
  40. HAVING COUNT(DISTINCT trainerID) = 1;