A sql lab filled with pokemon data

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #Part 3: Joins and Groups
  2. ## What is each pokemon's primary type?
  3. SELECT pok.name, t.name FROM pokemon.pokemons pok
  4. JOIN pokemon.types t ON pok.primary_type = t.id;
  5. ## What is Rufflet's secondary type?
  6. SELECT pok.name, t.name FROM pokemon.pokemons pok
  7. JOIN pokemon.types t
  8. ON pok.secondary_type = t.id
  9. WHERE pok.name = "Rufflet";
  10. ## What are the names of the pokemon that belong to the trainer with trainerID 303?
  11. SELECT trainer.trainername AS "Trainer", GROUP_CONCAT(pok.name) AS "Pokemons"
  12. FROM pokemon.pokemons pok
  13. JOIN pokemon.pokemon_trainer pokTrainer
  14. ON pokTrainer.pokemon_id = pok.id
  15. JOIN pokemon.trainers trainer
  16. ON pokTrainer.trainerID = trainer.trainerID
  17. WHERE trainer.trainerID = 303
  18. GROUP BY trainer.trainername;
  19. ## How many pokemon have a secondary type Poison
  20. SELECT COUNT(pok.secondary_type) AS "Poison Pokemons"
  21. FROM pokemon.pokemons pok
  22. JOIN pokemon.types types
  23. ON pok.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 type.name AS "Type", COUNT(pok.name) AS "Number of Pokemon"
  27. FROM pokemon.types type
  28. JOIN pokemon.pokemons pok
  29. ON pok.primary_type = type.id
  30. GROUP BY type.name;
  31. ## How many pokemon at level 100 does each trainer with at least one level 100 pokemon have?
  32. ## (Hint: your query should not display a trainer
  33. SELECT COUNT(trainer.pokemon_id) as "Pokemon at Level 100"
  34. FROM pokemon.pokemon_trainer trainer
  35. WHERE trainer.pokelevel = 100
  36. GROUP BY trainer.trainerID;
  37. ## How many pokemon only belong to one trainer and no other?
  38. SELECT COUNT(pokemon_id) AS "Pokemon with only one trainer"
  39. FROM (SELECT DISTINCT pokemon_id, COUNT(pokemon_id)
  40. FROM pokemon.pokemon_trainer
  41. GROUP BY pokemon_id HAVING COUNT(DISTINCT trainerID) = 1)alias;