导入hellodb.sql生成数据库,并进入到数据库之中mysql
mysql -uroot < hellodb.sql use mysql
(1)以ClassID分组,显示每班的同窗的人数
`select classid,count(stuid) from students group by classid``
(2)以Gender分组,显示其年龄之和
select gender,sum(age) from students group by gender
(3)以ClassID分组,显示其平均年龄大于25的班级
select classid,avg(age) as new_age from students group by classid having new_age>25
(4)以Gender分组,显示各组中年龄大于25的学员的年龄之和
select gender,sum(age) from students group by gender
(5)显示前5位同窗的姓名、课程及成绩
select name,course,score from (select name,score,courseid from (select * from students where stuid<=5) as s inner join scores on scores.stuid=s.stuid)as t inner join courses on courses.courseid=t.courseidsql
(6)显示其成绩高于80的同窗的名称及课程
select name,course from (select name,score,courseid from (select from scores where Score>80) as t inner join students on students.stuid=t.stuid) as t inner join courses on courses.courseid=t.courseid
(7)求前8位同窗每位同窗本身两门课的平均成绩,并按降序排列
select t.stuid,avg(score) from (select stuid,courseid from (select from students where stuid<=8) as s inner join coc on s.classid=coc.classid) as t inner join scores on s cores.stuid=t.stuid group by t.stuid
(8)取每位同窗各门课的平均成绩,显示成绩前三名的同窗的姓名和平均成绩
select name,avg(score) as 平均分数 from (select name,courseid from students inner join coc on students.classid=coc.classid) as s inner join scores on s.courseid=scores.courseid group by stuid order by 平均分数 desc limit 3
(9)显示每门课程课程名称及学习了这门课的同窗的个数
select courseid,count(CourseID) from students inner join coc on coc.classid=students.classid group by courseid数据库
(10)显示其年龄大于平均年龄的同窗的名字
select * from students where age>(select avg(age) from students)ide
(11)显示其学习的课程为第一、2,4或第7门课的同窗的名字
select name,courseid from (select * from coc where CourseID
in ('1','2','4','7')) as new inner join students on students.classid=new.classid学习
(12)显示其成员数最少为3个的班级的同窗中年龄大于同班同窗平均年龄的同窗
select * from (select name,classid,age from students) as s inner join (select new.classid,avg(age) as cc from (select classid from students group by ClassID having count(stuid) >= 3) as new inner join students on students.classid=new.classid group by new.classid) as a on a.classid=s.classid where cc<age;ui