创建表和数据

创建class表

create table class (
cid int(11) primary key auto_increment,
caption varchar(32) not null
); insert into class(caption) values("三年二班"),("三年三班"),("一年二班"),("二年九班"); select * from class;
+-----+--------------+
| cid | caption |
+-----+--------------+
| 1 | 三年二班 |
| 2 | 三年三班 |
| 3 | 一年二班 |
| 4 | 二年九班 |
+-----+--------------+

创建学生表

create table student(
sid int(11) primary key auto_increment,
gender char(1) not null,
class_id int(11) not null,
sname varchar(32) not null,
foreign key(class_id) references class(cid)
); insert into student values('1', '男', '1', '理解'), ('2', '女', '1', '钢蛋'), ('3', '男', '1', '张三'), ('4', '男', '1', '张一'), ('5', '女', '1', '张二'), ('6', '男', '1', '张四'), ('7', '女', '2', '铁锤'), ('8', '男', '2', '李三'), ('9', '男', '2', '李一'), ('10', '女', '2', '李二'), ('11', '男', '2', '李四'), ('12', '女', '3', '如花'), ('13', '男', '3', '刘三'), ('14', '男', '3', '刘一'), ('15', '女', '3', '刘二'), ('16', '男', '3', '刘四'); select * from student;
+-----+--------+----------+--------+
| sid | gender | class_id | sname |
+-----+--------+----------+--------+
| 1 | 男 | 1 | 理解 |
| 2 | 女 | 1 | 钢蛋 |
| 3 | 男 | 1 | 张三 |
| 4 | 男 | 1 | 张一 |
| 5 | 女 | 1 | 张二 |
| 6 | 男 | 1 | 张四 |
| 7 | 女 | 2 | 铁锤 |
| 8 | 男 | 2 | 李三 |
| 9 | 男 | 2 | 李一 |
| 10 | 女 | 2 | 李二 |
| 11 | 男 | 2 | 李四 |
| 12 | 女 | 3 | 如花 |
| 13 | 男 | 3 | 刘三 |
| 14 | 男 | 3 | 刘一 |
| 15 | 女 | 3 | 刘二 |
| 16 | 男 | 3 | 刘四 |
+-----+--------+----------+--------+

创建老师表

create table teacher(
tid int(11) primary key auto_increment,
tname varchar(32) not null
); insert into teacher values('1', '张磊老师'), ('2', '李平老师'), ('3', '刘海燕老师'), ('4', '朱云海老师'), ('5', '李杰老师'); select * from teacher;
+-----+-----------------+
| tid | tname |
+-----+-----------------+
| 1 | 张磊老师 |
| 2 | 李平老师 |
| 3 | 刘海燕老师 |
| 4 | 朱云海老师 |
| 5 | 李杰老师 |
+-----+-----------------+

创建课程表

create table course(
cid int(11) primary key auto_increment,
cname varchar(32) not null,
teacher_id int(11) not null,
foreign key(teacher_id) references teacher(tid)
); insert into course values('1', '生物', '1'), ('2', '物理', '2'), ('3', '体育', '3'), ('4', '美术', '2'); select * from course;
+-----+--------+------------+
| cid | cname | teacher_id |
+-----+--------+------------+
| 1 | 生物 | 1 |
| 2 | 物理 | 2 |
| 3 | 体育 | 3 |
| 4 | 美术 | 2 |
+-----+--------+------------+

创建成绩表

create table score(
sid int(11) primary key auto_increment,
student_id int(11) not null,
course_id int(11) not null,
num int(11) not null,
foreign key(student_id) references student(sid),
foreign key(course_id) references course(cid)
); insert into score values('1', '1', '1', '10'), ('2', '1', '2', '9'), ('5', '1', '4', '66'), ('6', '2', '1', '8'), ('8', '2', '3', '68'), ('9', '2', '4', '99'), ('10', '3', '1', '77'), ('11', '3', '2', '66'), ('12', '3', '3', '87'), ('13', '3', '4', '99'), ('14', '4', '1', '79'), ('15', '4', '2', '11'), ('16', '4', '3', '67'), ('17', '4', '4', '100'), ('18', '5', '1', '79'), ('19', '5', '2', '11'), ('20', '5', '3', '67'), ('21', '5', '4', '100'), ('22', '6', '1', '9'), ('23', '6', '2', '100'), ('24', '6', '3', '67'), ('25', '6', '4', '100'), ('26', '7', '1', '9'), ('27', '7', '2', '100'), ('28', '7', '3', '67'), ('29', '7', '4', '88'), ('30', '8', '1', '9'), ('31', '8', '2', '100'), ('32', '8', '3', '67'), ('33', '8', '4', '88'), ('34', '9', '1', '91'), ('35', '9', '2', '88'), ('36', '9', '3', '67'), ('37', '9', '4', '22'), ('38', '10', '1', '90'), ('39', '10', '2', '77'), ('40', '10', '3', '43'), ('41', '10', '4', '87'), ('42', '11', '1', '90'), ('43', '11', '2', '77'), ('44', '11', '3', '43'), ('45', '11', '4', '87'), ('46', '12', '1', '90'), ('47', '12', '2', '77'), ('48', '12', '3', '43'), ('49', '12', '4', '87'), ('52', '13', '3', '87'); select * from score;
+-----+------------+-----------+-----+
| sid | student_id | course_id | num |
+-----+------------+-----------+-----+
| 1 | 1 | 1 | 10 |
| 2 | 1 | 2 | 9 |
| 5 | 1 | 4 | 66 |
| 6 | 2 | 1 | 8 |
| 8 | 2 | 3 | 68 |
| 9 | 2 | 4 | 99 |
| 10 | 3 | 1 | 77 |
| 11 | 3 | 2 | 66 |
| 12 | 3 | 3 | 87 |
| 13 | 3 | 4 | 99 |
| 14 | 4 | 1 | 79 |
| 15 | 4 | 2 | 11 |
| 16 | 4 | 3 | 67 |
| 17 | 4 | 4 | 100 |
| 18 | 5 | 1 | 79 |
| 19 | 5 | 2 | 11 |
| 20 | 5 | 3 | 67 |
| 21 | 5 | 4 | 100 |
| 22 | 6 | 1 | 9 |
| 23 | 6 | 2 | 100 |
| 24 | 6 | 3 | 67 |
| 25 | 6 | 4 | 100 |
| 26 | 7 | 1 | 9 |
| 27 | 7 | 2 | 100 |
| 28 | 7 | 3 | 67 |
| 29 | 7 | 4 | 88 |
| 30 | 8 | 1 | 9 |
| 31 | 8 | 2 | 100 |
| 32 | 8 | 3 | 67 |
| 33 | 8 | 4 | 88 |
| 34 | 9 | 1 | 91 |
| 35 | 9 | 2 | 88 |
| 36 | 9 | 3 | 67 |
| 37 | 9 | 4 | 22 |
| 38 | 10 | 1 | 90 |
| 39 | 10 | 2 | 77 |
| 40 | 10 | 3 | 43 |
| 41 | 10 | 4 | 87 |
| 42 | 11 | 1 | 90 |
| 43 | 11 | 2 | 77 |
| 44 | 11 | 3 | 43 |
| 45 | 11 | 4 | 87 |
| 46 | 12 | 1 | 90 |
| 47 | 12 | 2 | 77 |
| 48 | 12 | 3 | 43 |
| 49 | 12 | 4 | 87 |
| 52 | 13 | 3 | 87 |
+-----+------------+-----------+-----+

题目

  1. 查询所有的课程的名称以及对应的任课老师姓名

    mysql> select cid,cname,tname from course left join teacher on teacher_id = tid order by cid;
    
    +-----+--------+-----------------+
    | cid | cname | tname |
    +-----+--------+-----------------+
    | 1 | 生物 | 张磊老师 |
    | 2 | 物理 | 李平老师 |
    | 3 | 体育 | 刘海燕老师 |
    | 4 | 美术 | 李平老师 |
    +-----+--------+-----------------+
  2. 查询学生表中男女生各有多少人

    mysql> select gender,count(*) as total from student group by gender;
    +--------+-------+
    | gender | total |
    +--------+-------+
    | 女 | 6 |
    | 男 | 10 |
    +--------+-------+
  3. 查询物理成绩等于100的学生的姓名

    select sname from student join score join course
    on
    student.sid = student_id and course_id = course.cid
    where cname = "物理" and num = 100;
    +--------+
    | sname |
    +--------+
    | 张四 |
    | 铁锤 |
    | 李三 |
    +--------+
  4. 查询平均成绩大于八十分的同学的姓名和平均成绩

    select sname,sum(num)/4 average_score from student join score
    on student_id = student.sid group by sname
    having sum(num)/4 > 80;
    +--------+---------------+
    | sname | average_score |
    +--------+---------------+
    | 张三 | 82.2500 |
    +--------+---------------+
  5. 查询所有学生的学号,姓名,选课数,总成绩

    select student.sid as sid,sname,count(*) count_course,sum(num) summary from student left join score on student_id = student.sid group by sname order by student.sid;
    +-----+--------+--------------+---------+
    | sid | sname | count_course | summary |
    +-----+--------+--------------+---------+
    | 1 | 理解 | 3 | 85 |
    | 2 | 钢蛋 | 3 | 175 |
    | 3 | 张三 | 4 | 329 |
    | 4 | 张一 | 4 | 257 |
    | 5 | 张二 | 4 | 257 |
    | 6 | 张四 | 4 | 276 |
    | 7 | 铁锤 | 4 | 264 |
    | 8 | 李三 | 4 | 264 |
    | 9 | 李一 | 4 | 268 |
    | 10 | 李二 | 4 | 297 |
    | 11 | 李四 | 4 | 297 |
    | 12 | 如花 | 4 | 297 |
    | 13 | 刘三 | 1 | 87 |
    | 14 | 刘一 | 1 | NULL |
    | 15 | 刘二 | 1 | NULL |
    | 16 | 刘四 | 1 | NULL |
    +-----+--------+--------------+---------+
  6. 查询姓李老师的个数

    select group_concat(tname) tname,count(*) count from teacher where tname like "李%";
    +---------------------------+-------+
    | tname | count |
    +---------------------------+-------+
    | 李平老师,李杰老师 | 2 |
    +---------------------------+-------+
  7. 查询没有报李平老师课的学生姓名

    select sname from student where sid not in (
    select student_id from score where course_id in (
    select cid from course where teacher_id in (
    select tid from teacher where tname = "李平老师"
    )
    )
    );
    +--------+
    | sname |
    +--------+
    | 刘三 |
    | 刘一 |
    | 刘二 |
    | 刘四 |
    +--------+
  8. 查询物理课程比生物课程高的学生的学号

    select t1.student_id from( 
    
     (select * from score where course_id in (select cid from course where cname = '生物')) t1  
    
    left join 
    
     (select * from score where course_id in (select cid from course where cname = '物理')) t2  
    
    on  t1.student_id = t2.student_id) 
    
    where t1.num < t2.num;
    
    +------------+
    | student_id |
    +------------+
    | 6 |
    | 7 |
    | 8 |
    +------------+
  9. 查询没有同时选修物理课程和体育课程的学生姓名

    select sname from student where sid not in (
    select student_id from score where course_id in (
    select cid from course where cname in ("物理","体育")
    ) group by student_id having count(*) = 2
    ); +--------+
    | sname |
    +--------+
    | 理解 |
    | 钢蛋 |
    | 刘三 |
    | 刘一 |
    | 刘二 |
    | 刘四 |
    +--------+
  10. 查询挂科超过两门(包括两门)的学生姓名和班级

    select sname,caption from student join class on cid = class_id
    where sid=(select student_id from score where num < 60 group by student_id having count(*) >= 2);
    +--------+--------------+
    | sname | caption |
    +--------+--------------+
    | 理解 | 三年二班 |
    +--------+--------------+
  11. 查询选修了所有课程的学生姓名

    select sname from student where sid in (
    select student_id from score group by student_id having count(*) = 4
    );
    +--------+
    | sname |
    +--------+
    | 张三 |
    | 张一 |
    | 张二 |
    | 张四 |
    | 铁锤 |
    | 李三 |
    | 李一 |
    | 李二 |
    | 李四 |
    | 如花 |
    +--------+
  12. 查询李平老师教的课程的所有成绩记录

    select num from score where course_id in (
    select cid from course where teacher_id = (
    select tid from teacher where tname = "李平老师"
    )
    );
    +-----+
    | num |
    +-----+
    | 9 |
    | 66 |
    | 11 |
    | 11 |
    | 100 |
    | 100 |
    | 100 |
    | 88 |
    | 77 |
    | 77 |
    | 77 |
    | 66 |
    | 99 |
    | 99 |
    | 100 |
    | 100 |
    | 100 |
    | 88 |
    | 88 |
    | 22 |
    | 87 |
    | 87 |
    | 87 |
    +-----+
  13. 查询全部学生都选修了的课程号和课程名

    select cid,cname from course where cid in(
    select course_id from score group by course_id having count(*) = 4
    );
    Empty set (0.00 sec)
  14. 查询每门课程被选修的次数

    select cname,count(*) from course join score on cid = course_id group by course_id;
    +--------+----------+
    | cname | count(*) |
    +--------+----------+
    | 生物 | 12 |
    | 物理 | 11 |
    | 体育 | 12 |
    | 美术 | 12 |
    +--------+----------+
  15. 查询之选修了一门课程的学生姓名和学号

    select sname,sid from student where sid in(
    select student_id from score group by student_id having count(*) = 1
    );
    +--------+-----+
    | sname | sid |
    +--------+-----+
    | 刘三 | 13 |
    +--------+-----+
  16. 查询所有学生考出的成绩并按从高到低排序(成绩去重)

    select distinct num from score order by num desc;
    +-----+
    | num |
    +-----+
    | 100 |
    | 99 |
    | 91 |
    | 90 |
    | 88 |
    | 87 |
    | 79 |
    | 77 |
    | 68 |
    | 67 |
    | 66 |
    | 43 |
    | 22 |
    | 11 |
    | 10 |
    | 9 |
    | 8 |
    +-----+
  17. 查询平均成绩大于85的学生姓名和平均成绩

    select sname,avg(num) average_score from student join score
    on student_id = student.sid group by sname
    having avg(num) > 85;
    +--------+---------------+
    | sname | average_score |
    +--------+---------------+
    | 刘三 | 87.0000 |
    +--------+---------------+
  18. 查询生物成绩不及格的学生姓名和对应生物分数

    select sname,num from student join score on student.sid = student_id
    where num < 60 and course_id = (
    select cid from course where cname = "生物"
    );
    +--------+-----+
    | sname | num |
    +--------+-----+
    | 理解 | 10 |
    | 钢蛋 | 8 |
    | 张四 | 9 |
    | 铁锤 | 9 |
    | 李三 | 9 |
    +--------+-----+
  19. 查询在所有选修了李平老师课程的学生中,这些课程(李平老师的课程,不是所有课程)平均成绩最高的学生姓名

    select sname,avg(num) from student join score join course join teacher on
    student.sid = student_id and course.cid = course_id and teacher_id = tid where
    tname = "李平老师" group by student_id order by avg(num) desc limit 1; +--------+----------+
    | sname | avg(num) |
    +--------+----------+
    | 张四 | 100.0000 |
    +--------+----------+
  20. 查询每门课程成绩最好的前两名学生姓名

    (select cname,sname from course join score join student
    on course_id = course.cid and student.sid = student_id
    where cname = "生物" order by num desc limit 2)
    union
    (select cname,sname from course join score join student
    on course_id = course.cid and student.sid = student_id
    where cname = "物理" order by num desc limit 2)
    union
    (select cname,sname from course join score join student
    on course_id = course.cid and student.sid = student_id
    where cname = "美术" order by num desc limit 2)
    union
    (select cname,sname from course join score join student
    on course_id = course.cid and student.sid = student_id
    where cname = "体育" order by num desc limit 2) +--------+--------+
    | cname | sname |
    +--------+--------+
    | 生物 | 李一 |
    | 生物 | 如花 |
    | 物理 | 李三 |
    | 物理 | 铁锤 |
    | 美术 | 张四 |
    | 美术 | 张二 |
    | 体育 | 刘三 |
    | 体育 | 张三 |
    +--------+--------+
  21. 查询不同课程但成绩相同的学号,课程号,成绩

    select distinct s1.course_id,s2.course_id,s1.num,s2.num from score as s1, score as s2 where s1.num = s2.num and s1.course_id != s2.course_id;
    
    +-----------+-----------+-----+-----+
    | course_id | course_id | num | num |
    +-----------+-----------+-----+-----+
    | 1 | 2 | 9 | 9 |
    | 2 | 4 | 66 | 66 |
    | 2 | 1 | 77 | 77 |
    | 4 | 2 | 66 | 66 |
    | 4 | 3 | 87 | 87 |
    | 2 | 4 | 100 | 100 |
    | 2 | 1 | 9 | 9 |
    | 4 | 2 | 100 | 100 |
    | 2 | 4 | 88 | 88 |
    | 4 | 2 | 88 | 88 |
    | 1 | 2 | 77 | 77 |
    | 3 | 4 | 87 | 87 |
    +-----------+-----------+-----+-----+
  22. 查询没学过“叶平”老师课程的学生姓名以及选修的课程名称;

    select sname,cname from student join score join course on
    student.sid = student_id and course.cid = course_id
    where student.sid not in (
    select distinct student_id from score where course_id in(
    select cid from course where teacher_id in (
    select tid from teacher where tname = "李平老师"
    )
    )
    ); +--------+--------+
    | sname | cname |
    +--------+--------+
    | 刘三 | 体育 |
    +--------+--------+
  23. 查询所有选修了学号为1的同学选修过的一门或者多门课程的同学学号和姓名;

    select sid,sname from student where sid in (
    select student_id from score where course_id in(
    select course_id from score where student_id = 1
    )
    ); +-----+--------+
    | sid | sname |
    +-----+--------+
    | 1 | 理解 |
    | 2 | 钢蛋 |
    | 3 | 张三 |
    | 4 | 张一 |
    | 5 | 张二 |
    | 6 | 张四 |
    | 7 | 铁锤 |
    | 8 | 李三 |
    | 9 | 李一 |
    | 10 | 李二 |
    | 11 | 李四 |
    | 12 | 如花 |
    +-----+--------+
  24. 任课最多的老师中学生单科成绩最高的学生姓名

    select sname from student join score join course on student_id = student.sid and course_id = cid where teacher_id = (
    select teacher_id from (select teacher_id,count(*) as total from course group by teacher_id order by total desc limit 1) as t
    )
    group by course_id having max(num);

MySQL练手小试题的更多相关文章

  1. 【Python】【辅助程序】练手小程序:记录外网动态IP地址

    练手小程序 程序作用:对IP实时记录: 1.定时获取外网IP,存储在本地文件中: 编写思路: 1)收集获取外网的API接口       http://bbs.125.la/thread-1383897 ...

  2. 【Python精华】100个Python练手小程序

    100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同 ...

  3. 整理了适合新手的20个Python练手小程序

    100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. 本文附带基础视频教程:私信回复[基础]就可以获取的 [程序1] ...

  4. 初始Spring MVC——练手小项目

    初始Spring MVC 前几天开始了我的spring学习之旅,由于之前使用过MVC模式来做项目,所以我先下手的是 Spring MVC,做个练手项目,非常简单 项目介绍: 用户输入信息 -> ...

  5. vue练手小项目--眼镜在线试戴

    最近看到了一个眼镜在线试戴小项目使用纯js手写的,本人刚学习vue.js没多久,便试试用vue做做看了,还没完善. 其中包括初始图片加载,使用keywords查找,父子组件之间传递信息,子组件之间传递 ...

  6. Spring+Mybatis整合的练手小项目(一)项目部署

    声明:教程是网上找的,代码是自己敲的 项目目录大致如下: 1. 首先创建Maven工程,在pom.xml中加入项目所需依赖: <?xml version="1.0" enco ...

  7. 前端练手小项目——网页版qq音乐仿写

    qq音乐网页版仿写 一些步骤与注意事项 一开始肯定就是html+css布局和页面了,这段特别耗时间,耐心写完就好了 首先要说一下大致流程: 一定要先布局html!,所以一定要先分析页面布局情况,用不同 ...

  8. 练手小游戏(代码篇之敌人AI

    诶呀~又没有更新微博,去拔牙了,疼死了,休息了几天过来接着写代码~ 首先是Base.写了一个框架,照别人扒的. Base分三部分,AILocomotion(AI移动),Steering(行为基类),V ...

  9. 开源一个练手小App, PrintableCheckList

    A small but powerful App, only focus on one thing, make you easy to print out your checklist. It is ...

随机推荐

  1. 1、kafka概述

    一.关于消息队列 消息队列是一种应用间的通信方式,消息就是是指在应用之间传送的数据,它也是进程通信的一种重要的方式. 1.消息队列的基本架构 producer:消息生产者. broker:消息处理中心 ...

  2. e3s10 网络管理

    1. host上设置 iptables -t nat -A POSTROUTING -o eno1 -j MASQUERADE # https://www.unixtutorial.org/how-t ...

  3. ImageView.ScaleType

    前言 对ImageView.ScaleType,学习安卓需掌握.以官方链接:http://android.xsoftlab.net/reference/android/widget/ImageView ...

  4. shell脚本编程基础之函数

    函数 作用:代码重用 定义函数: 方法1: function FUNCTION_NAME { #函数名和定义变量名一样,只能包含数字字母下划线,并且不能以数字开头 command } 方法2: FUN ...

  5. 去参加了十四届D2前端大会~

    朋友喊我去一起去d2,原来一直在加班,没有想去的动力,后来业务上线,幸运的入手了别人转的一张票(也不便宜啊)- 讲了五个挑战 端侧渲染体系的重塑,从PC时代到无线时代,再到未来的IOT时代,在渲染方面 ...

  6. HTML页面之间的参数传递

    HTML 与 HTML 的跳转中如何在HTML之中实现参数的传递?主要代码如下:request为方法名称,params 为要获取的参数. function request(params) { var ...

  7. Python3 fake_useragent 模块的使用和报错解决方案

    在使用 Python 做爬虫的时候,我们需要伪装头部信息骗过网站的防爬策略,Python 中的第三方模块 fake_useragent 就很好的解决了这个问题,它将给我们返回一个随机封装了好的头部信息 ...

  8. 创建批处理文件.bat文件(删除指定文件夹下的文件及文件夹并循环)

    1.针对仅仅是删除文件夹下的文件的操作:使用del命令,单纯的删除文件操作,如下:del /f /s /q C:\Users\dell\AppData\Local\Temp\*.* 2.删除文件夹操作 ...

  9. composer.json和composer.lock有什么区别?

    我们在做项目的时候,总是要安装一些依赖.composer给我们提供了很多方便.直接运行composer install.   当我们运行composer install 将会读取composer.lo ...

  10. <div> <p> <span>的用法和区别

    <div> 标签可以把文档分割为独立的.不同的部分.它可以用作严格的组织工具,并且不使用任何格式与其关联. 更重要的意义是在网页的动态实现过程中,对划分的区域统一处理,例如换背景色.字体等 ...