A sql lab filled with pokemon data

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #What is each pokemon's primary type?
  2. select pokemons.name, pokemons.primary_type from Pokemon.pokemons
  3. select * from pokemon.pokemons p join types t on p.primary = t.id;
  4. #What is Rufflet's secondary type
  5. 18
  6. SELECT name, secondary_type from Pokemon.pokemons where name = "Rufflet"
  7. #What are the names of the pokemon that belong to the trainer with trainerID 303?
  8. Wailord and Vileplume
  9. select distinct (po.name) as Pokemon, pt.trainerID as TrainerID,
  10. pk.trainername as Trainername from pokemon.trainers pk
  11. left join pokemon.pokemon_trainer pt ON pk.trainerID = pt.trainerID
  12. left join Pokemon.pokemons po ON po.id=pt.pokemon_id
  13. where pk.trainerID = 303;
  14. #How many pokemon have a secondary type Poison
  15. 31
  16. select count(pokemons.id) from pokemon.pokemons
  17. LEFT JOIN Pokemon.types ON types.id = pokemons.secondary_type
  18. where types.name = "Poison";
  19. #What are all the primary types and how many pokemon have that type?
  20. 656 pokemons
  21. select distinct pokemons.name, Pokemon.types.name from pokemons
  22. join types on pokemons.primary_type = Pokemon.types.id
  23. select t.name, count(p.name) from pokemon.pokemons p
  24. join types t
  25. on p.primary_type = t.id
  26. group by t.name
  27. #How many pokemon at level 100 does each trainer with at least one level 100 pokemon have?
  28. 963
  29. SELECT count(pp.name) AS numberofpokemon, pt.pokelevel FROM pokemon.pokemon_trainer pt
  30. LEFT JOIN pokemon.pokemons pp ON pt.pokemon_id = pp.id
  31. WHERE pt.pokelevel = 100 GROUP BY pt.pokelevel;
  32. SELECT trainername, t.trainerID, COUNT(pt.pokelevel) FROM pokemon_trainer pt
  33. JOIN trainers t
  34. ON pt.trainerID = t.trainerID
  35. WHERE pokelevel = 100
  36. GROUP BY 2,1;
  37. // 2 = second field, 1 = first field
  38. #How many pokemon only belong to one trainer and no other?
  39. 14
  40. Select COUNT(*) AS "Unique"
  41. FROM (SELECT DISTINCT pokemon_id, COUNT(pokemon_id)
  42. FROM pokemon.pokemon_trainer
  43. GROUP BY pokemon_id
  44. HAVING COUNT(DISTINCT trainerID)=1) alias;