A sql lab filled with pokemon data

Queries 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*How many types of pokemon can a pokemon have?*/
  2. SELECT COUNT(*) FROM pokemon.types;
  3. /*What is the name of the pokemon with id 45?*/
  4. SELECT * FROM pokemon.pokemons WHERE id LIKE '45';
  5. /*How many pokemon are there?*/
  6. SELECT COUNT(*) FROM pokemon.pokemons;
  7. /*How many types are there?*/
  8. SELECT COUNT(DISTINCT primary_type) FROM pokemons;
  9. /*How many pokemon have a secondary type?*/
  10. SELECT COUNT(secondary_type) From pokemons;
  11. /*What is each pokemon's primary type?*/
  12. SELECT p.name, t.name
  13. FROM pokemons p
  14. JOIN types t
  15. ON p.primary_type = t.id;
  16. /*What is Rufflet's secondary type?*/
  17. SELECT t.name
  18. FROM pokemons p
  19. JOIN types t
  20. ON p.primary_type = t.id
  21. WHERE p.name LIKE 'Rufflet';
  22. /*What are the names of the pokemon that belong to the trainer with trainerID 303?*/
  23. SELECT p.name
  24. FROM pokemons p
  25. JOIN pokemon_trainer pt
  26. ON pt.pokemon_id=p.id
  27. JOIN trainers t
  28. ON t.trainerID=pt.trainerID
  29. WHERE t.trainerID = '303';
  30. /*How many pokemon have a secondary type Poison*/
  31. SELECT p.name
  32. FROM pokemons p
  33. JOIN types t
  34. ON p.secondary_type = t.id
  35. Where t.name = 'Poison';
  36. /*What are all the primary types and how many pokemon have that type?*/
  37. SELECT t.name, COUNT(p.name)
  38. FROM pokemons p
  39. JOIN types t
  40. ON p.primary_type = t.id
  41. GROUP BY p.primary_type;
  42. /*How many pokemon at level 100 does each trainer with at least one level 100 pokemon have?*/
  43. Select pt.trainerID, pt.pokelevel
  44. FROM pokemon_trainer pt
  45. WHERE pt.pokelevel=100
  46. GROUP BY pt.trainerID;
  47. /* How many pokemon only belong to one trainer and no other?*/
  48. Select COUNT(pokemon_id)
  49. FROM (
  50. SELECT pt.pokemon_id
  51. FROM pokemon_trainer pt
  52. GROUP BY pt.pokemon_id
  53. HAVING COUNT(*)=1
  54. ) AS once;
  55. SELECT pokemons.name, trainers.trainername, pokemon_trainer.pokelevel, , pokemons.secondary_type
  56. FROM pokemons
  57. JOIN