MySQL练习题

一、导入hellodb.sql生成数据库

mysql -p < hellodb.sql
  1. 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
select name,age from students where age>25 and gender='M';
  1. 以ClassID为分组依据,显示每组的平均年龄
select classid,avg(age) as 'average age' from students group by classid;
  1. 显示第2题中平均年龄大于30的分组及平均年龄
select classid,avg(age) as 'average age' from students group by classid having avg(age)>30;

4显示以L开头的名字的同学的信息

select * from students where name like 'L%';

5显示TeacherID非空的同学的相关信息

select * from students where name like 'L%';

6以年龄排序后,显示年龄最大的前10位同学的信息

select * from students order by age desc limit 10;
# sqlserver 中使用top 函数,但是MySQL支持这种语法
select top 10 * from students order by age desc;

7查询年龄大于等于20岁,小于等于25岁的同学的信息

select * from students where age>=20 and age<=25;
select * from students where age between 20 and 25;

8以ClassID分组,显示每班的同学的人数

select classid,count(stuid) as 'sum of students' from students group by classid;

9以Gender分组,显示其年龄之和

select classid,count(stuid) as 'sum of students' from students group by classid;

10以Gender分组,显示其年龄之和

select gender,sum(age) from students group by gender;

11以ClassID分组,显示其平均年龄大于25的班级

select classid,avg(age) from students group by classid having avg(age)>25;

12以Gender分组,显示各组中年龄大于25的学员的年龄之和

select gender,sum(age) from students where age>25 group by gender;

13 显示前5位同学的姓名、课程及成绩

select name,course,score from students inner join scores inner join courses on students.stuid=scores.stuid and scores.courseid=courses.courseid order by score desc limit 5;

14显示其成绩高于80的同学的名称及课程;

select name,course,score from students inner join scores inner join courses on students.stuid=scores.stuid and scores.courseid=courses.courseid where score>80;
# 另一种方法:
select name,course,score 
from students,scores,courses 
where students.stuid=scores.stuid and scores.courseid=courses.courseid and score>80;

15 求前8位同学每位同学自己两门课的平均成绩,并按降序排列

# 增加sum列的目的是过滤只学了一门课的同学
select stuid,avg(score),sum(score) from scores group by stuid having avg(score)<sum(score) order by avg(score) desc limit 8;

16显示每门课程课程名称及学习了这门课的同学的个数

select course,sum(stuid) from students 
inner join scores inner join courses on students.stuid=scores.stuid and scores.courseid=courses.courseid 
group by course;

# 另一种
select course,sum(stuid) from scores inner join courses on scores.courseid=courses.courseid group by scores.courseid;

17 如何显示其年龄大于平均年龄的同学的名字

select name,age from students where age > (select avg(age) from students);

18 如何显示其学习的课程为第1、2,4或第7门课的同学的名字

select name,courseid from students inner join scores on students.stuid=scores.stuid where courseid in (1,2,4,7);

19 如何显示其成员数最少为3个的班级的同学中年龄大于同班同学平均年龄的同学

select * from students as a
inner join (select classid,count(stuid),avg(age) as aage from students where classid >0 group by classid having count(stuid)>=3 ) as b on a.classid=b.classid
where a.age>b.aage;

# 将复杂的查询分解为一个一个小的查询
select classid,count(classid),avg(age) from students group by classid having count(classid)>=3; 
+---------+----------------+----------+
| classid | count(classid) | avg(age) |
+---------+----------------+----------+
|       1 |              4 |  20.5000 |
|       2 |              3 |  36.0000 |
|       3 |              5 |  32.2000 |
|       4 |              4 |  24.7500 |
|       6 |              4 |  20.7500 |
|       7 |              3 |  19.6667 |
+---------+----------------+----------+
select * from students where classid=1 and age>20.5;
select * from students where classid=2 and age>36.0;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值