A sql lab filled with pokemon data

Part 3: Joins & Groups.sql 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #What is each pokemons primary type?
  2. SELECT p.name, t.name
  3. FROM pokemon.pokemons p
  4. JOIN pokemon.types t
  5. ON p.primary_type = t.id;
  6. #What is Rufflets secondary type?
  7. SELECT p.name, t.name
  8. FROM pokemon.pokemons p
  9. JOIN pokemon.types t
  10. ON p.secondary_type = t.id
  11. WHERE p.name = 'Rufflet';
  12. #What are the names of the pokemon that belong to the trainer with trainerID 303?
  13. SELECT t.trainername as "Trainer", GROUP_CONCAT(p.name) as "Pokemons"
  14. FROM pokemon.pokemons p
  15. JOIN pokemon.pokemon_trainer pt
  16. ON pt.pokemon_id = p.id
  17. JOIN pokemon.trainers t
  18. ON pt.trainerID = t.trainerID
  19. WHERE t.trainerID = 303
  20. GROUP BY t.trainername;
  21. #How many pokemon have a secondary type Poison?
  22. SELECT COUNT(*)
  23. FROM pokemon.pokemons p
  24. JOIN pokemon.types t
  25. ON p.secondary_type = t.id
  26. WHERE t.name = 'Poison';
  27. #What are all the primary types and how many pokemon have that type?
  28. SELECT t.name, COUNT(p.name)
  29. FROM pokemon.types t
  30. JOIN pokemon.pokemons p
  31. ON p.primary_type = t.id
  32. GROUP BY t.name;
  33. #How many pokemon at level 100 does each trainer with at least one level 100 pokemon have?
  34. SELECT t.trainername as "Trainer", pt.pokelevel
  35. FROM pokemon.trainers t
  36. JOIN pokemon.pokemon_trainer pt
  37. ON pt.trainerID = t.trainerID
  38. WHERE pt.pokelevel = 100
  39. GROUP BY t.trainername;
  40. #How many pokemon only belong to one trainer and no other?
  41. SELECT p.name as "Pokemon",
  42. COUNT(t.trainername) as "Trainers"
  43. FROM pokemon.pokemons p
  44. JOIN pokemon.pokemon_trainer pt
  45. ON pt.pokemon_id = p.id
  46. JOIN pokemon.trainers t
  47. ON pt.trainerID = t.trainerID
  48. GROUP BY p.name
  49. HAVING COUNT(t.trainername) = 1;