human
Schema:
Student (StuID, LName, Fname, Age, Sex, Major, Advisor, city_code)
Dorm (dormid, dorm_name, student_capacity, gender)
Dorm_amenity (amenid, amenity_name)
Has_amenity (dormid, amenid)
Lives_in (stuid, dormid, room_number)
Please answer with SQL.
assistant
Sure, I understand.
human
How many dorms are there?
assistant
SELECT count(*) FROM dorm
human
Find the first name of students who are living in the dorm that has most number of amenities.
assistant
SELECT T1.fname FROM student AS T1 JOIN lives_in AS T2 ON T1.stuid = T2.stuid WHERE T2.dormid IN (SELECT T2.dormid FROM dorm AS T3 JOIN has_amenity AS T4 ON T3.dormid = T4.dormid JOIN dorm_amenity AS T5 ON T4.amenid = T5.amenid GROUP BY T3.dormid ORDER BY count(*) DESC LIMIT 1)
human
Find the name of dorms that do not have any amenity
assistant
SELECT dorm_name FROM dorm WHERE dormid NOT IN (SELECT dormid FROM has_amenity)
human
Find the number of dorms that have some amenity.
assistant
SELECT count(DISTINCT dormid) FROM has_amenity
human
Find the name of amenity that is most common in all dorms.
assistant
SELECT T1.amenity_name FROM dorm_amenity AS T1 JOIN has_amenity AS T2 ON T1.amenid = T2.amenid GROUP BY T2.amenid ORDER BY count(*) DESC LIMIT 1
human
What are the different dorm amenity names in alphabetical order?
assistant
SELECT amenity_name FROM dorm_amenity ORDER BY amenity_name ;