MySQL的练习题

一、导入hellodb.sql生成数据库
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| scores            |
| students          |
| teachers          |
| toc               |
+-------------------+
students 表的数据如下:
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |
|     4 | Ding Dian     |  32 | M      |       4 |         4 |
|     5 | Yu Yutong     |  26 | M      |       3 |         1 |
|     6 | Shi Qing      |  46 | M      |       5 |      NULL |
|     7 | Xi Ren        |  19 | F      |       3 |      NULL |
|     8 | Lin Daiyu     |  17 | F      |       7 |      NULL |
|     9 | Ren Yingying  |  20 | F      |       6 |      NULL |
|    10 | Yue Lingshan  |  19 | F      |       3 |      NULL |
|    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |
|    12 | Wen Qingqing  |  19 | F      |       1 |      NULL |
|    13 | Tian Boguang  |  33 | M      |       2 |      NULL |
|    14 | Lu Wushuang   |  17 | F      |       3 |      NULL |
|    15 | Duan Yu       |  19 | M      |       4 |      NULL |
|    16 | Xu Zhu        |  21 | M      |       1 |      NULL |
|    17 | Lin Chong     |  25 | M      |       4 |      NULL |
|    18 | Hua Rong      |  23 | M      |       7 |      NULL |
|    19 | Xue Baochai   |  18 | F      |       6 |      NULL |
|    20 | Diao Chan     |  19 | F      |       7 |      NULL |
|    21 | Huang Yueying |  22 | F      |       6 |      NULL |
|    22 | Xiao Qiao     |  20 | F      |       1 |      NULL |
|    23 | Ma Chao       |  23 | M      |       4 |      NULL |
|    24 | Xu Xian       |  27 | M      |    NULL |      NULL |
|    25 | Sun Dasheng   | 100 | M      |    NULL |      NULL |
+-------+---------------+-----+--------+---------+-----------+
  • 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
select name,age from students where age>25 and gender='M';
  • 以ClassID为分组依据,显示每组的平均年龄
select classid,avg(age) as 'average age' from students group by classid;
  • 显示第2题中平均年龄大于30的分组及平均年龄
select classid,avg(age) as 'average age' from students group by classid having avg(age)>30;
  • 显示以L开头的名字的同学的信息
select * from students where name like 'L%';
  • 显示TeacherID非空的同学的相关信息
select * from students where teacherid is not null;
# 另一种方式(推荐)
select * from students where teacherid > 0;
  • 以年龄排序后,显示年龄最大的前10位同学的信息
select * from students order by age desc limit 10;
# sqlserver 中使用top 函数,但是MySQL支持这种语法
select top 10 * from students order by age desc;

  • 查询年龄大于等于20岁,小于等于25岁的同学的信息
select * from students where age>=20 and age<=25;
select * from students where age between 20 and 25;
  • 以ClassID分组,显示每班的同学的人数
select classid,count(stuid) as 'sum of students' from students group by classid;
  • 以Gender分组,显示其年龄之和
select gender,sum(age) from students group by gender;
  • 以ClassID分组,显示其平均年龄大于25的班级
select classid,avg(age) from students group by classid having avg(age)>25;
  • 以Gender分组,显示各组中年龄大于25的学员的年龄之和
select gender,sum(age) from students where age>25 group by gender;
  • 显示前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;
  • 显示其成绩高于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;
  • 求前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;
  • 显示每门课程课程名称及学习了这门课的同学的个数
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;
  • 如何显示其年龄大于平均年龄的同学的名字
select name,age from students where age > (select avg(age) from students);
  • 如何显示其学习的课程为第1、2,4或第7门课的同学的名字
select name,courseid from students inner join scores on students.stuid=scores.stuid where courseid in (1,2,4,7);
  • 如何显示其成员数最少为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;

  • 统计各班级中年龄大于全校同学平均年龄的同学
select * from students where age > (select avg(age) from students );
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值