Database: pets_1
# student ( stuid , lname , fname , age , sex , major , advisor , city_code )
# has_pet ( stuid , petid )
# pets ( petid , pettype , pet_age , weight )
# has_pet.stuid = student.stuid
# has_pet.petid = pets.petid

Question: Find the number of pets whose weight is heavier than 10.
SQL: select count ( * ) from pets where weight > 10

Question: How many pets have a greater weight than 10?
SQL: select count ( * ) from pets where weight > 10

Question: Find the weight of the youngest dog.
SQL: select weight from pets order by pet_age asc limit 1

Question: How much does the youngest dog weigh?
SQL: select weight from pets order by pet_age asc limit 1

Question: Find the maximum weight for each type of pet. List the maximum weight and pet type.
SQL: select max ( weight ) , pettype from pets group by pettype

Question: List the maximum weight and type for each type of pet.
SQL: select max ( weight ) , pettype from pets group by pettype

Question: Find number of pets owned by students who are older than 20.
SQL: select count ( * ) from student join has_pet on student.stuid = has_pet.stuid where student.age > 20

Question: How many pets are owned by students that have an age greater than 20?
SQL: select count ( * ) from student join has_pet on student.stuid = has_pet.stuid where student.age > 20

Question: Find the number of dog pets that are raised by female students (with sex F).
SQL: select count ( * ) from student join has_pet on student.stuid = has_pet.stuid join pets on has_pet.petid = pets.petid where student.sex = 'F' and pets.pettype = 'dog'

Question: How many dog pets are raised by female students?
SQL: select count ( * ) from student join has_pet on student.stuid = has_pet.stuid join pets on has_pet.petid = pets.petid where student.sex = 'F' and pets.pettype = 'dog'

Question: Find the number of distinct type of pets.
SQL: select count ( distinct pettype ) from pets

Question: How many different types of pet are there?
SQL: select count ( distinct pettype ) from pets

Question: Find the first name of students who have cat or dog pet.
SQL: select distinct student.fname from student join has_pet on student.stuid = has_pet.stuid join pets on pets.petid = has_pet.petid where pets.pettype = 'cat' or pets.pettype = 'dog'

Question: What are the first names of every student who has a cat or dog as a pet?
SQL: select distinct student.fname from student join has_pet on student.stuid = has_pet.stuid join pets on pets.petid = has_pet.petid where pets.pettype = 'cat' or pets.pettype = 'dog'

Question: Find the first name of students who have both cat and dog pets .
SQL: select student.fname from student join has_pet on student.stuid = has_pet.stuid join pets on pets.petid = has_pet.petid where pets.pettype = 'cat' intersect select student.fname from student join has_pet on student.stuid = has_pet.stuid join pets on pets.petid = has_pet.petid where pets.pettype = 'dog'

Question: What are the students' first names who have both cats and dogs as pets?
SQL: select student.fname from student join has_pet on student.stuid = has_pet.stuid join pets on pets.petid = has_pet.petid where pets.pettype = 'cat' intersect select student.fname from student join has_pet on student.stuid = has_pet.stuid join pets on pets.petid = has_pet.petid where pets.pettype = 'dog'

Question: Find the major and age of students who do not have a cat pet.
SQL: select major , age from student where stuid not in ( select student.stuid from student join has_pet on student.stuid = has_pet.stuid join pets on pets.petid = has_pet.petid where pets.pettype = 'cat' )

Question: What major is every student who does not own a cat as a pet, and also how old are they?
SQL: select major , age from student where stuid not in ( select student.stuid from student join has_pet on student.stuid = has_pet.stuid join pets on pets.petid = has_pet.petid where pets.pettype = 'cat' )

Question: Find the id of students who do not have a cat pet.
SQL: select stuid from student except select student.stuid from student join has_pet on student.stuid = has_pet.stuid join pets on pets.petid = has_pet.petid where pets.pettype = 'cat'

Question: What are the ids of the students who do not own cats as pets?
SQL: select stuid from student except select student.stuid from student join has_pet on student.stuid = has_pet.stuid join pets on pets.petid = has_pet.petid where pets.pettype = 'cat'

Question: Find the first name and age of students who have a dog but do not have a cat as a pet.
SQL: select student.fname , student.age from student join has_pet on student.stuid = has_pet.stuid join pets on pets.petid = has_pet.petid where pets.pettype = 'dog' and student.stuid not in ( select student.stuid from student join has_pet on student.stuid = has_pet.stuid join pets on pets.petid = has_pet.petid where pets.pettype = 'cat' )

Question: What is the first name of every student who has a dog but does not have a cat?
SQL: select student.fname , student.age from student join has_pet on student.stuid = has_pet.stuid join pets on pets.petid = has_pet.petid where pets.pettype = 'dog' and student.stuid not in ( select student.stuid from student join has_pet on student.stuid = has_pet.stuid join pets on pets.petid = has_pet.petid where pets.pettype = 'cat' )

Question: Find the type and weight of the youngest pet.
SQL: select pettype , weight from pets order by pet_age asc limit 1

Question: What type of pet is the youngest animal, and how much does it weigh?
SQL: select pettype , weight from pets order by pet_age asc limit 1

Question: Find the id and weight of all pets whose age is older than 1.
SQL: select petid , weight from pets where pet_age > 1

Question: What is the id and weight of every pet who is older than 1?
SQL: select petid , weight from pets where pet_age > 1

Question: Find the average and maximum age for each type of pet.
SQL: select avg ( pet_age ) , max ( pet_age ) , pettype from pets group by pettype

Question: What is the average and maximum age for each pet type?
SQL: select avg ( pet_age ) , max ( pet_age ) , pettype from pets group by pettype

Question: Find the average weight for each pet type.
SQL: select avg ( weight ) , pettype from pets group by pettype

Question: What is the average weight for each type of pet?
SQL: select avg ( weight ) , pettype from pets group by pettype

Question: Find the first name and age of students who have a pet.
SQL: select distinct student.fname , student.age from student join has_pet on student.stuid = has_pet.stuid

Question: What are the different first names and ages of the students who do have pets?
SQL: select distinct student.fname , student.age from student join has_pet on student.stuid = has_pet.stuid

Question: Find the id of the pet owned by student whose last name is 'Smith'.
SQL: select has_pet.petid from student join has_pet on student.stuid = has_pet.stuid where student.lname = 'Smith'

Question: What is the id of the pet owned by the student whose last name is 'Smith'?
SQL: select has_pet.petid from student join has_pet on student.stuid = has_pet.stuid where student.lname = 'Smith'

Question: Find the number of pets for each student who has any pet and student id.
SQL: select count ( * ) , student.stuid from student join has_pet on student.stuid = has_pet.stuid group by student.stuid

Question: For students who have pets , how many pets does each student have ? list their ids instead of names .
SQL: select count ( * ) , student.stuid from student join has_pet on student.stuid = has_pet.stuid group by student.stuid

Question: Find the first name and gender of student who have more than one pet.
SQL: select student.fname , student.sex from student join has_pet on student.stuid = has_pet.stuid group by student.stuid having count ( * ) > 1

Question: What is the first name and gender of the all the students who have more than one pet?
SQL: select student.fname , student.sex from student join has_pet on student.stuid = has_pet.stuid group by student.stuid having count ( * ) > 1

Question: Find the last name of the student who has a cat that is age 3.
SQL: select student.lname from student join has_pet on student.stuid = has_pet.stuid join pets on pets.petid = has_pet.petid where pets.pet_age = 3 and pets.pettype = 'cat'

Question: What is the last name of the student who has a cat that is 3 years old?
SQL: select student.lname from student join has_pet on student.stuid = has_pet.stuid join pets on pets.petid = has_pet.petid where pets.pet_age = 3 and pets.pettype = 'cat'

Question: Find the average age of students who do not have any pet .
SQL: select avg ( age ) from student where stuid not in ( select stuid from has_pet )

Question: What is the average age for all students who do not own any pets ?
SQL: select avg ( age ) from student where stuid not in ( select stuid from has_pet )