human
Schema:
Allergy_Type (Allergy, AllergyType)
Has_Allergy (StuID, Allergy)
Student (StuID, LName, Fname, Age, Sex, Major, Advisor, city_code)
Please answer with SQL.
assistant
Sure, I understand.
human
List the first and last name of the students who do not have any food type allergy.
assistant
SELECT fname , lname FROM Student WHERE StuID NOT IN (SELECT T1.StuID FROM Has_allergy AS T1 JOIN Allergy_Type AS T2 ON T1.Allergy = T2.Allergy WHERE T2.allergytype = 'food')
human
Show all allergies with number of students affected.
assistant
SELECT Allergy , count(*) FROM Has_allergy GROUP BY Allergy
human
Show all allergy type with number of students affected.
assistant
SELECT T2.allergytype , count(*) FROM Has_allergy AS T1 JOIN Allergy_type AS T2 ON T1.allergy = T2.allergy GROUP BY T2.allergytype
human
Find the last name and age of the student who has allergy to both milk and cat.
assistant
SELECT lname , age FROM Student WHERE StuID IN (SELECT StuID FROM Has_allergy WHERE Allergy = 'Milk' INTERSECT SELECT StuID FROM Has_allergy WHERE Allergy = 'Cat')
human
Find the number of students who are older than 18 and do not have allergy to either food or animal.
assistant
SELECT count(*) FROM Student WHERE age > 18 AND StuID NOT IN ( SELECT StuID FROM Has_allergy AS T1 JOIN Allergy_Type AS T2 ON T1.Allergy = T2.Allergy WHERE T2.allergytype = 'food' OR T2.allergytype = 'animal')
human
Find the average age of the students who have allergies with food and animal types.
assistant
SELECT avg ( age ) FROM Student WHERE StuID IN ( SELECT T1.StuID FROM Has_allergy AS T1 JOIN Allergy_Type AS T2 ON T1.Allergy = T2.Allergy WHERE T2.allergytype = "food" INTERSECT SELECT T1.StuID FROM Has_allergy AS T1 JOIN Allergy_Type AS T2 ON T1.Allergy = T2.Allergy WHERE T2.allergytype = "animal" ) ;