SQL Lab 8
# 1. Show all the animals (id, name, sex)
prepare query1 from 'select id, name, sex from animal';
execute query1;
# 2. Show all the animals by ? race_id (id, name, sex, dob, race_name)
prepare query2 from 'select animal.id, animal.name, animal.sex, animal.dob, race.name
from animal join race on race.id = animal.race_id
where race_id = ?';
set @r_id = '1';
execute query2 using @r_id;
# Show all the animals by ? race_id and ? species_id (id, name, sex, race_name, species_name)
prepare query3 from 'select animal.id, animal.name, animal.sex, animal.dob, race.name, species.current_name
from animal join race on race.id = animal.race_id join species on species.id = animal.species_id
where animal.race_id = ? and animal.species_id = ?';
set @r_id = 1, @s_id = 1;
execute query3 using @r_id, @s_id;
# 4. Which specie has less than ? males and ? females (id, name, sex, race_name, species_name) where ? is a number of animals
prepare query4 from 'select animal.id, animal.name, animal.sex, animal.dob, race.name, species.current_name, count(*)
from animal join race on race.id = animal.race_id join species on species.id = animal.species_id
where animal.sex = ? or animal.sex = ?
group by sex, species.current_name
having count(*) < @int';
set @r_id = 1, @s_id = 1, @a_m = 'M', @a_f = 'F', @int = '10';
execute query4 using @a_f, @int;
select animal.id, animal.name, animal.sex, animal.dob, race.name, species.current_name, count(*)
from animal join race on race.id = animal.race_id join species on species.id = animal.species_id
where animal.sex = 'M' or animal.sex = 'F'
group by sex,species.current_name
having count(*) < 10;
select species.current_name, animal.id, animal.name, animal.sex, race.name
from animal join race on race.id = animal.race_id join species on species.id = animal.species_id
where sex = 'M' < 1 and sex = 'F' < 3;
SELECT animal.sex, species.latin_name
FROM animal
INNER JOIN species
ON species.id = animal.species_id
GROUP BY latin_name
HAVING COUNT(*) < 5;
# 5. Assuming the inflation rises 20% more every year, show the prices of ? animals for the ? year based on specie's race (id, name, sex, race_name, species_name)
# where first argument is the number of animals to show and the second argument is the year number
select animal.id, animal.name, animal.sex, race.name, species.current_name,
species.price + (species.price * 20.0 / 100.0)*1 as 'year1',
species.price + (species.price * 20.0 / 100.0)*2 as 'year2',
species.price + (species.price * 20.0 / 100.0)*3 as 'year3',
species.price + (species.price * 20.0 / 100.0)*4 as 'year4',
species.price + (species.price * 20.0 / 100.0)*5 as 'year5'
from animal join race on race.id = animal.race_id join species on species.id = animal.species_id
where animal.name = ? and year = ?;
prepare query5 from 'select animal.id, animal.name, animal.sex, animal.dob, race.name, species.current_name, species.price + (species.price * 20.0 / 100.0)*@year
from animal join race on race.id = animal.race_id join species on species.id = animal.species_id
where animal.name = ?';
set @a_name = 'rox', @year=2;
execute query5 using @a_name;
Comments
Post a Comment