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
Show all allergies and their types.
assistant
SELECT allergy , allergytype FROM Allergy_type
human
What are the student ids of students who don't have any allergies?
assistant
SELECT StuID FROM Student EXCEPT SELECT StuID FROM Has_allergy
human
Which allergy has most number of students affected?
assistant
SELECT Allergy FROM Has_allergy GROUP BY Allergy ORDER BY count(*) DESC LIMIT 1
human
Show all student IDs who have at least two allergies.
assistant
SELECT StuID FROM Has_allergy GROUP BY StuID HAVING count(*) >= 2
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
Show all allergies with number of students affected.
assistant
SELECT Allergy , COUNT( * ) FROM Has_allergy GROUP BY Allergy ;