A sql lab filled with pokemon data

part2 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #1-What are all the types of pokemon that a pokemon can have?
  2. create schema pokemon;
  3. select NAME
  4. from pokemon.types
  5. #2-What is the name of the pokemon with id 45?
  6. create schema pokemon;
  7. SELECT NAME
  8. FROM pokemon.pokemons
  9. WHERE id=45
  10. #3-How many pokemon are there?
  11. SELECT COUNT(id)
  12. FROM pokemon.pokemons;
  13. #4-How many types are there?
  14. SELECT distinct(name)
  15. FROM pokemon.types;
  16. #5-How many pokemon have a secondary type?
  17. SELECT COUNT(*)
  18. FROM pokemon.pokemons
  19. where secondary_type is not null
  20. #6-What is each pokemon's primary type?
  21. SELECT pokemons.name as Pokemon_Name,types.name as Primary_Name
  22. from pokemon.pokemons, pokemon.types
  23. where primary_type=types.id;
  24. #7-What is Rufflet's secondary type?
  25. SELECT types.name
  26. from pokemon.pokemons, pokemon.types
  27. where secondary_type=types.id
  28. and pokemon.pokemons.name ="Rufflet";
  29. #8-What are the names of the pokemon that belong to the trainer with trainerID 303?
  30. select pokemons.name
  31. from pokemon.pokemons, pokemon.pokemon_trainer
  32. where pokemon_trainer.pokemon_id=pokemons.id
  33. and pokemon.pokemon_trainer.trainerID=303;
  34. #9-How many pokemon have a secondary type Poison
  35. SELECT count(*)
  36. from pokemon.pokemons, pokemon.types
  37. where secondary_type=types.id
  38. and types.name="Poison";
  39. #10-What are all the primary types and how many pokemon have that type?
  40. select types.name, count(*)
  41. from pokemon.pokemons, pokemon.types
  42. where pokemons.primary_type=types.id
  43. group by types.name;
  44. #11-How many pokemon at level 100 does each trainer with at least one level 100 pokemone have? (Hint: your query should not display a trainer
  45. SELECT COUNT(*),pokemon.pokemons.name
  46. FROM pokemon.pokemon_trainer, pokemon.pokemons
  47. WHERE pokemon.pokemon_trainer.pokemon_id= pokemon.pokemons.id
  48. AND pokemon.pokemon_trainer.pokelevel= 100
  49. GROUP BY name
  50. HAVING COUNT(*) >= 1;
  51. #12-How many pokemon only belong to one trainer and no other?
  52. SELECT name, COUNT(DISTINCT pokemon.pokemon_trainer.trainerID)
  53. FROM pokemon.pokemon_trainer, pokemon.pokemons
  54. WHERE pokemon.pokemon_trainer.pokemon_id= pokemon.pokemons.id
  55. GROUP BY name
  56. HAVING COUNT(DISTINCT pokemon.pokemon_trainer.trainerID)=1;
  57. #FINAL REPORT PART 4
  58. SELECT pokemon.pokemons.name AS Pokemon_Name,pokemon.trainers.trainername AS Trainer_Name,pokemon.pokemon_trainer.pokelevel AS Level,PT.name AS Primary_Type, ST.name AS Secondary_Type
  59. FROM pokemon.pokemon_trainer,pokemon.pokemons, pokemon.types AS PT ,pokemon.types AS ST,pokemon.trainers
  60. WHERE pokemon.pokemons.id=pokemon.pokemon_trainer.pokemon_id
  61. AND pokemon.pokemons.primary_type=PT.id
  62. AND pokemon.pokemons.secondary_type=ST.id
  63. AND pokemon.pokemon_trainer.trainerID=pokemon.trainers.trainerID
  64. ORDER BY pokelevel DESC,
  65. PT.name ASC , ST.name ASC, trainername asc ;
  66. # The strongest trainer is the one who trains pokemon of highest level and then sorted in ascending order of primary level, secondary level and the trainer name
  67. # Using above stated criterion the strongest trainer is - "Bandana Guy Kaler" as returned by above query as first record