Linux 第五次练习(MySQL-DQL语句练习)
导入hellodb.sql生成数据库
1. 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
select name,age from students where age>25 and Gender="M";
2. 以ClassID为分组依据,显示每组的平均年龄
select avg(age) from students group by classid;
3. 显示第2题中平均年龄大于30的分组及平均年龄
select avg(age),classid from students group by classid having (avg(age)>30);
4. 显示以L开头的名字的同学的信息
select * from students where name like 'l%';
5. 显示TeacherID非空的同学的相关信息
select * from students where TeacherID is not null;
6. 以年龄排序后,显示年龄最大的前10位同学的信息
select * from students order by age DESC limit 10;
7. 查询年龄大于等于20岁,小于等于25岁的同学的信息
select * from students where age>=20 and age<=25;
8. 以ClassID分组,显示每班的同学的人数
select ClassID,count(*) 学生人数 from students group by ClassID;
9. 以Gender分组,显示其年龄之和
select Gender,sum(age) from students group by Gender;
10. 以ClassID分组,显示其平均年龄大于25的班级
select ClassID,avg(age) from students group by ClassID having (avg(age)>25);
11. 以Gender分组,显示各组中年龄大于25的学员的年龄之和
select Gender,sum(age) from students where age>25 group by Gender;
12. 显示前5位同学的姓名、课程及成绩
select st.name,co.Course,sc.Score from students st inner join scores sc on st.StuID=sc.StuID inner join courses co on sc.CourseID=co.CourseID where st.StuID<=5;
13. 显示其成绩高于80的同学的名称及课程
select st.name,co.Course,sc.Score from students st inner join scores sc on st.StuID=sc.StuID inner join courses co on sc.CourseID=co.CourseID where sc.Score>80;
14. 取每位同学各门课的平均成绩,显示成绩前三名的同学的姓名和平均成绩
select st.name,avg(sc.Score) 平均成绩 from students st inner join courses co on st.ClassID=co.CourseID inner join scores sc on st.StuID=sc.StuID group by st.name order by avg(sc.Score) DESC limit 3;
15. 显示每门课程课程名称及学习了这门课的同学的个数
select co.Course,count(st.name) from students st inner join courses co on st.ClassID=co.CourseID group by co.Course;
16. 显示其年龄大于平均年龄的同学的名字
select name from students where age>(select avg(age)from students);
17. 显示其学习的课程为第1、2,4或第7门课的同学的名字
select name from students where ClassID in (1,2,4,7);
18. 显示其成员数最少为3个的班级的同学中年龄大于同班同学平均年龄的同学
没搞懂
select st.name,st.age,avg.平均年龄 from students st,(select classid,count(*),avg(age) 平均年龄 from students group by classid having count(*)>=3) avg where st.classid=avg.classid and st.age > avg.平均年龄;
19. 统计各班级中年龄大于全校同学平均年龄的同学
select name,ClassID 班级 from students where age>(select avg(age) from students) order by 班级

浙公网安备 33010602011771号