文章目录
- 1. 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
- 2. 以ClassID为分组依据,显示每组的平均年龄
- 3. 显示第2题中平均年龄大于30的分组及平均年龄
- 4. 显示以L开头的名字的同学的信息
- 5. 显示TeacherID非空的同学的相关信息
- 6. 以年龄排序后,显示年龄最大的前10位同学的信息
- 7. 查询年龄大于等于20岁,小于等于25岁的同学的信息
- 8. 以ClassID分组,显示每班的同学的人数
- 9. 以Gender分组,显示其年龄之和
- 10. 以ClassID分组,显示其平均年龄大于25的班级
- 11. 以Gender分组,显示各组中年龄大于25的学员的年龄之和
- 12. 显示前5位同学的姓名、课程及成绩
- 13. 显示其成绩高于80的同学的名称及课程
- 14. 取每位同学各门课的平均成绩,显示成绩前三名的同学的姓名和平均成绩
- 15. 显示每门课程课程名称及学习了这门课的同学的个数
- 16. 显示其年龄大于平均年龄的同学的名字
- 17. 显示其学习的课程为第1、2,4或第7门课的同学的名字
- 18. 显示其成员数最少为3个的班级的同学中年龄大于同班同学平均年龄的同学
- 19. 统计各班级中年龄大于全校同学平均年龄的同学
练习:导入hellodb.sql生成数据库
1. 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
select name 名字,age 年龄 from students where age>25 and gender='M';
2. 以ClassID为分组依据,显示每组的平均年龄
select classid 班级id,avg(age) 平均年龄 from students group by classid order by classid;
3. 显示第2题中平均年龄大于30的分组及平均年龄
select classid 班级id,avg(age) 平均年龄 from students group by 班级id having 平均年龄>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岁的同学的信息
方法1:
select * from students where age between 20 and 25;
方法2:
select * from students where age>=20 and age<=25;
8. 以ClassID分组,显示每班的同学的人数
select classid 班级id,count(stuid) 每个班的人数 from students group by classid;
9. 以Gender分组,显示其年龄之和
select gender,sum(age) from students group by gender;
10. 以ClassID分组,显示其平均年龄大于25的班级
select classid 班级id,avg(age) 平均年龄 from students group by classid having avg(age)>25;ng avg(age)>25;
11. 以Gender分组,显示各组中年龄大于25的学员的年龄之和
select gender,sum(age) from students where age>25 group by gender;
12. 显示前5位同学的姓名、课程及成绩
select name 姓名,course 课程,score 成绩 from students st inner join scores sc on st.stuid=sc.stuid inner join courses co on sc.courseid=co.courseid limit 5;
13. 显示其成绩高于80的同学的名称及课程
select name 姓名,course 课程,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 name 姓名,avg(score) 平均成绩 from students st inner join scores sc on st.stuid=sc.stuid group by 姓名 order by 平均成绩 desc limit 3;
15. 显示每门课程课程名称及学习了这门课的同学的个数
select course 课程,count(stuid) 人数 from scores sc inner join courses co on n sc.courseid=co.courseid group by course;
16. 显示其年龄大于平均年龄的同学的名字
select name 姓名 from students where age>(select avg(age) from students);
17. 显示其学习的课程为第1、2,4或第7门课的同学的名字
select name 姓名,courseid 课程名 from scores sc inner join students st on st.stuid=sc.stuid; t.stuid=sc.stuid;
18. 显示其成员数最少为3个的班级的同学中年龄大于同班同学平均年龄的同学
select st1.name 姓名,st1.age 年龄,st2.classid 班级id,st2.平均年龄 from students st1 cross join (select classid,count(stuid),avg(age) 平均年龄 from students group by classid having count(stuid)>3) st2 where st1.age>st2.平均年龄 and st1.classid=st2.classid;
19. 统计各班级中年龄大于全校同学平均年龄的同学
select classid 班级id,count(stuid) 各班级中年龄大于平均年龄的人数 from students whents where age>(select avg(age) from students) group by classid;