A sql lab filled with pokemon data

PokeQueries 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. ### Simple Selects and Counts
  2. * What are all the types of pokemon that a pokemon can have?
  3. SELECT t.name FROM pokemon.types t;
  4. * What is the name of the pokemon with id 45?
  5. SELECT t.name FROM pokemon.pokemons t WHERE id=45;
  6. * How many pokemon are there?
  7. SELECT COUNT(*) as "count" FROM pokemon.pokemons;
  8. * How many types are there?
  9. SELECT COUNT(*) as "count" from pokemon.types;
  10. * How many pokemon have a secondary type?
  11. SELECT COUNT(secondary_type) as "count" from pokemon.pokemons WHERE secondary_type IS NOT NULL;
  12. ### Joins and Groups
  13. * What is each pokemon's primary type?
  14. SELECT p.name as "Pokemon", t.name as "Primary Type" from pokemon.pokemons p JOIN pokemon.types t ON p.primary_type = t.id;
  15. * What is Rufflet's secondary type?
  16. SELECT p.name, t.name from pokemon.pokemons p JOIN pokemon.types t ON p.secondary_type = t.id WHERE p.name="Rufflet";
  17. * What are the names of the pokemon that belong to the trainer with trainerID 303?
  18. SELECT p.name from pokemon.pokemons p JOIN pokemon.pokemon_trainer t ON p.id = t.pokemon_id WHERE t.trainerID=303;
  19. * How many pokemon have a secondary type `Poison`?
  20. SELECT p.name as "Pokemon", t.name as "Secondary Type" from pokemon.pokemons p JOIN pokemon.types t ON p.secondary_type = t.id WHERE t.name="Poison";
  21. * What are all the primary types and how many pokemon have that type?
  22. SELECT t.name as "Type", COUNT(p.id) as "PokeCount" FROM pokemon.types t JOIN pokemon.pokemons p ON p.primary_type = t.id GROUP BY t.name;
  23. * How many pokemon at level 100 does each trainer with at least one level 100 pokemon have? (Hint: your query should not display a trainer)
  24. SELECT COUNT(pokelevel) as "The Count" from pokemon.pokemon_trainer WHERE pokelevel>=100 GROUP BY trainerID;
  25. * How many pokemon only belong to one trainer and no other?
  26. SELECT p.name, COUNT(t.pokemon_id) as "trainer count" from pokemon.pokemon_trainer t JOIN pokemon.pokemons p ON t.pokemon_id = p.id GROUP BY pokemon_id HAVING COUNT(DISTINCT trainerID) = 1;