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
Find the number of students for the cities where have more than one student.
assistant
SELECT count(*) , city_code FROM student GROUP BY city_code HAVING count(*) > 1
human
Find the total number of students living in the male dorm (with gender M).
assistant
SELECT count(*) FROM student AS T1 JOIN lives_in AS T2 ON T1.stuid = T2.stuid JOIN dorm AS T3 ON T3.dormid = T2.dormid WHERE T3.gender = 'M'
human
Find the number of students whose age is older than the average age for each gender.
assistant
SELECT count(*) , sex FROM student WHERE age > (SELECT avg(age) FROM student) GROUP BY sex
human
Find the first name of students who is older than 20.
assistant
SELECT fname FROM student WHERE age > 20
human
Find the average age of students living in each dorm and the name of dorm.
assistant
SELECT avg(T1.age) , T3.dorm_name FROM student AS T1 JOIN lives_in AS T2 ON T1.stuid = T2.stuid JOIN dorm AS T3 ON T3.dormid = T2.dormid GROUP BY T3.dorm_name
human
Find the number of students who is older than 20 in each dorm.
assistant
SELECT COUNT( * ) , T3.dorm_name FROM student AS T1 JOIN lives_in AS T2 ON T1.stuid = T2.stuid JOIN dorm AS T3 ON T3.dormid = T2.dormid WHERE T1.age > 20 GROUP BY T3.dorm_name ;