day_35作业

  1. 查询所有大于60分的学生的姓名和学号 (DISTINCT: 去重)

    select student.sname,score.sid from score left join student on student_id = student.sid where number >60;
    
    mysql> select student.sname,score.sid from score left join student on student_id = student.sid where number >60;
    +-------+-----+
    | sname | sid |
    +-------+-----+
    | 铁锤  |   3 |
    +-------+-----+
    

-- 2.查询每个老师教授的课程数量 和 老师信息

select teacher.tid,teacher.tname,count(course.teacher_id) from  teacher left join course on teacher.tid=teacher_id group by course.teacher_id;


mysql> select teacher.tid,teacher.tname,count(course.teacher_id) from  teacher left join course on teacher.tid=teacher_id group by course.teacher_id;
+-----+--------+--------------------------+
| tid | tname  | count(course.teacher_id) |
+-----+--------+--------------------------+
|   1 | 波多   |                        2 |
|   2 | 苍空   |                        1 |
|   3 | 饭岛   |                        1 |
|   4 | 张磊   |                        2 |
|   5 | 李平   |                        1 |
|   6 | 刘海燕 |                        1 |
|   7 | 朱云海 |                        1 |
|   8 | 李杰   |                        1 |
+-----+--------+--------------------------+

-- 3. 查询学 生的信息以及学生所在的班级信息

select sid,sname,gender,class.caption from student left join class on class_id =class.cid;

mysql> select student.sid,student.sname,student.gender,class.caption from student left join class on class_id =class.cid;
+-----+-------+--------+----------+
| sid | sname | gender | caption  |
+-----+-------+--------+----------+
|   1 | 钢蛋  | 女     | 三年二班 |
|   2 | 铁锤  | 女     | 三年二班 |
|   3 | 山炮  | 男     | 一年三班 |
+-----+-------+--------+----------+

-- 4、学生中男生的个数和女生的个数

select gender,count(gender) from student group by gender;

mysql> select student.sid,student.sname,student.gender,class.caption from student left join class on class_id =class.cid;
+-----+-------+--------+----------+
| sid | sname | gender | caption  |
+-----+-------+--------+----------+
|   1 | 钢蛋  | 女     | 三年二班 |
|   2 | 铁锤  | 女     | 三年二班 |
|   3 | 山炮  | 男     | 一年三班 |
+-----+-------+--------+----------+

-- 5、获取所有学习'生物'的学生的学号和成绩;姓名

select student.sid,student.sname,number from student left join score on student.sid = student_id left join course on course.cid=corse_id where course.cid =1;


mysql> select student.sid,student.sname,number from student left join score on student.sid = student_id left join course on course.cid=corse_id where course.cid =1;
+-----+-------+--------+
| sid | sname | number |
+-----+-------+--------+
|   1 | 钢蛋  |     60 |
+-----+-------+--------+

-- 6、查询平均成绩大于60分的同学的学号和平均成绩;

select student.sid,avg(number) from score left join student on student_id=student.sid group by student.sid having avg(number)>60;

mysql> select student.sid,avg(number) from score left join student on student_id=student.sid group by student.sid having avg(number)>60;
+------+-------------+
| sid  | avg(number) |
+------+-------------+
|    1 |     72.2500 |
|    2 |     67.5000 |
|    3 |     93.3333 |
+------+-------------+

-- 7、查询姓“李”的老师的个数;

select count(tname) as '李姓老师的个数' from teacher where tname like '李%';


mysql> select count(tname) as '李姓老师的个数' from teacher where tname like '李%';
+----------------+
| 李姓老师的个数 |
+----------------+
|            2 |
+----------------+

-- 8、查询课程成绩小于60分的同学的学号、姓名;

select student.sid,student.sname from student left join score on student.sid=student_id where number<60;

mysql> select student.sid,student.sname from student left join score on student.sid=student_id where number<60;
+-----+-------+
| sid | sname |
+-----+-------+
|   1 | 钢蛋  |
|   2 | 铁锤  |
+-----+-------+

-- 9. 删除学习“叶平”老师课的SC表记录

delete from teacher where tname ='叶平';


mysql> select * from teacher;
+-----+--------+
| tid | tname  |
+-----+--------+
|   1 | 波多   |
|   2 | 苍空   |
|   3 | 饭岛   |
|   4 | 张磊   |
|   5 | 李平   |
|   6 | 刘海燕 |
|   7 | 朱云海 |
|   8 | 李杰   |
|   9 | 叶平   |
+-----+--------+


mysql> delete from teacher where tname ='叶平';
Query OK, 1 row affected (0.10 sec)
mysql> select * from teacher;
+-----+--------+
| tid | tname  |
+-----+--------+
|   1 | 波多   |
|   2 | 苍空   |
|   3 | 饭岛   |
|   4 | 张磊   |
|   5 | 李平   |
|   6 | 刘海燕 |
|   7 | 朱云海 |
|   8 | 李杰   |
+-----+--------+

-- 10.查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;

select corse_id,max(number),min(number) from score left join course on course.cid=corse_id group by corse_id;

mysql> select corse_id,max(number),min(number) from score left join course on course.cid=corse_id group by corse_id;
+----------+-------------+-------------+
| corse_id | max(number) | min(number) |
+----------+-------------+-------------+
|        1 |          60 |          60 |
|        2 |         100 |          59 |
|        3 |          80 |          80 |
|        4 |          90 |          90 |
|        5 |          70 |          70 |
|        6 |          80 |          80 |
|        7 |          80 |          80 |
|        8 |         100 |         100 |
|        9 |          20 |          20 |
|       10 |         100 |         100 |
+----------+-------------+-------------+

-- 11.查询每门课程被选修的学生数

select cname,count(student_id) from course left join score on corse_id=course.cid group by score.corse_id;

mysql> select cname,count(student_id) from course left join score on corse_id=course.cid group by score.corse_id;
+----------+-------------------+
| cname    | count(student_id) |
+----------+-------------------+
| 生物     |                 1 |
| 体育     |                 2 |
| 物理     |                 1 |
| 英语     |                 1 |
| 数学     |                 1 |
| 科学     |                 1 |
| 美术     |                 1 |
| 思想品德 |                 1 |
| 语文     |                 1 |
| 地理     |                 1 |
+----------+-------------------+

-- 12.查询姓“张”的学生名单;

select * from student where sname like '张%';

mysql> select * from student where sname like '张%';
+-----+--------+--------+----------+
| sid | sname  | gender | class_id |
+-----+--------+--------+----------+
|   4 | 张全蛋 | 男     |        3 |
+-----+--------+--------+----------+

-- 13.查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列

select corse_id,avg(number) from score right join course on corse_id = course.cid group by score.corse_id order by avg(number),course.cid desc ;

mysql> select corse_id,avg(number) from score right join course on corse_id = course.cid group by score.corse_id order by avg(number),course.cid desc ;
+----------+-------------+
| corse_id | avg(number) |
+----------+-------------+
|        9 |     20.0000 |
|        1 |     60.0000 |
|        5 |     70.0000 |
|        2 |     79.5000 |
|        7 |     80.0000 |
|        6 |     80.0000 |
|        3 |     80.0000 |
|        4 |     90.0000 |
|       10 |    100.0000 |
|        8 |    100.0000 |
+----------+-------------+

-- 14.查询平均成绩大于85的所有学生的学号、姓名和平均成绩

select student.sid,student.sname,avg(number) from student left join score on student.sid=student_id having avg(number) > 85;

-- 15.查询课程编号为3且课程成绩在80分以上的学生的学号和姓名;

select student.sid,student.sname from student left join score student.sid=student_id where corse_id =3 and number >80;

-- 16.查询各个课程及相应的选修人数

select course.cname,count(score.student_id) from course left join score on corse_id=course.cid group by course.cname;

+----------+-------------------------+
| cname    | count(score.student_id) |
+----------+-------------------------+
| 体育     |                       2 |
| 地理     |                       1 |
| 思想品德 |                       1 |
| 数学     |                       1 |
| 物理     |                       1 |
| 生物     |                       1 |
| 科学     |                       1 |
| 美术     |                       1 |
| 英语     |                       1 |
| 语文     |                       1 |
+----------+-------------------------+

-- 17.查询“4”课程分数小于60,按分数降序排列的同学学号

select student.sid from student right join score on student.sid=student_id where corse_id = 4 and number <60 order by student.sid desc;

-- 18.删除学号为“2”的同学的“1”课程的成绩

delete from score where student_id =2 and corse_id =1;
# 班级
create table class(
cid int auto_increment primary key,
caption varchar(32) not null default ''
)charset utf8;

# 插入数据
insert into class(caption) values('三年二班'),('一年三班'),('三年一班');


# 学生
create table student(
sid int auto_increment primary key,
sname varchar(32) not null default '',
gender char(2) not null default '',
class_id int not null default 1,
constraint fk_cls_stu foreign key (class_id)references class(cid)
)charset utf8;

# 插入数据
insert into student(sname,gender,class_id) values ('钢蛋','女',1),('铁锤','女',1),('山炮','男',2);


# 老师
create table teacher(
tid int auto_increment primary key,
tname varchar(32) not null default ''
)charset utf8;

# 插入数据
insert into teacher(tname) values('波多'),('苍空'),('饭岛');


# 课程
create table course(
cid int auto_increment primary key,
cname varchar(32) not null default '',
teacher_id int not null default 1,

constraint fk_teacher_course foreign key (teacher_id) references teacher(tid)
)charset utf8;

# 插入数据
insert into course(cname,teacher_id)values('生物',1),('体育',1),('物理',2),('英语',3),('数学',4),('科学',4),('美术',6),('思想品德',7),('语文',5),('地理',8);


# 成绩
create table score(
sid int auto_increment primary key,
student_id int not null,
corse_id int not null,
number int not null,

constraint fk_stu_sco foreign key (student_id) references student(sid),
constraint fk_cor_sco foreign key (corse_id) references course(cid)
)charset utf8;

# 插入数据
insert into score(student_id,corse_id,number)values(1,1,60),(1,2,59),(2,2,100);
原文地址:https://www.cnblogs.com/maqiaobin/p/11768011.html