一、查询数据
学生表 Student
create table Student(SId varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10));
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-12-20' , '男');
insert into Student values('04' , '李云' , '1990-12-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-01-01' , '女');
insert into Student values('07' , '郑竹' , '1989-01-01' , '女');
insert into Student values('09' , '张三' , '2017-12-20' , '女');
insert into Student values('10' , '李四' , '2017-12-25' , '女');
insert into Student values('11' , '李四' , '2012-06-06' , '女');
insert into Student values('12' , '赵六' , '2013-06-13' , '女');
insert into Student values('13' , '孙七' , '2014-06-01' , '女');
科目表 Course
create table Course(CId varchar(10),Cname nvarchar(10),TId varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
教师表 Teacher
create table Teacher(TId varchar(10),Tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
成绩表 SC
create table SC(SId varchar(10),CId varchar(10),score decimal(18,1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);
二、问题
11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
从SC表中选取score小于60的,并group by sid,having count 大于1,然后再在sc表中,查询平均成绩,最后与student表联合查询。
mysql> select student.SId, student.Sname,b.avg
-> from student RIGHT JOIN
-> (select sid, AVG(score) as avg from sc
-> where sid in (
-> select sid from sc
-> where score<60
-> GROUP BY sid
-> HAVING count(score)>1)
-> GROUP BY sid) b on student.sid=b.sid;
+------+--------+----------+
| SId | Sname | avg |
+------+--------+----------+
| 04 | 李云 | 33.33333 |
| 06 | 吴兰 | 32.50000 |
+------+--------+----------+
2 rows in set (0.06 sec)
12.检索" 01 "课程分数小于 60,按分数降序排列的学生信息
这道题简单,就不多说了。
mysql> select student.*, sc.score from student, sc
-> where student.sid = sc.sid
-> and sc.score < 60
-> and cid = "01"
-> order by sc.score desc;
+------+--------+---------------------+------+-------+
| SId | Sname | Sage | Ssex | score |
+------+--------+---------------------+------+-------+
| 04 | 李云 | 1990-12-06 00:00:00 | 男 | 50.0 |
| 06 | 吴兰 | 1992-01-01 00:00:00 | 女 | 31.0 |
+------+--------+---------------------+------+-------+
2 rows in set (0.00 sec)
13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
我的评价是,逻辑上没多大难度,写上也没有多少难度
mysql> select * from sc
-> left join (
-> select sid,avg(score) as avscore from sc
-> group by sid
-> )r
-> on sc.sid = r.sid
-> order by avscore desc;
+------+------+-------+------+----------+
| SId | CId | score | sid | avscore |
+------+------+-------+------+----------+
| 07 | 02 | 89.0 | 07 | 93.50000 |
| 07 | 03 | 98.0 | 07 | 93.50000 |
| 01 | 03 | 99.0 | 01 | 89.66667 |
| 01 | 02 | 90.0 | 01 | 89.66667 |
| 01 | 01 | 80.0 | 01 | 89.66667 |
| 05 | 02 | 87.0 | 05 | 81.50000 |
| 05 | 01 | 76.0 | 05 | 81.50000 |
| 03 | 01 | 80.0 | 03 | 80.00000 |
| 03 | 03 | 80.0 | 03 | 80.00000 |
| 03 | 02 | 80.0 | 03 | 80.00000 |
| 02 | 02 | 60.0 | 02 | 70.00000 |
| 02 | 01 | 70.0 | 02 | 70.00000 |
| 02 | 03 | 80.0 | 02 | 70.00000 |
| 04 | 01 | 50.0 | 04 | 33.33333 |
| 04 | 03 | 20.0 | 04 | 33.33333 |
| 04 | 02 | 30.0 | 04 | 33.33333 |
| 06 | 01 | 31.0 | 06 | 32.50000 |
| 06 | 03 | 34.0 | 06 | 32.50000 |
+------+------+-------+------+----------+
18 rows in set (0.00 sec)
14.查询各科成绩最高分、最低分和平均分:
以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率,及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
这道题看起来要求很多,但从下面代码可以看出来,其实,这就是一个单表查询,其难点在于限制条件的书写。
mysql> select
-> sc.CId ,
-> max(sc.score)as 最高分,
-> min(sc.score)as 最低分,
-> AVG(sc.score)as 平均分,
-> count(*)as 选修人数,
-> sum(case when sc.score>=60 then 1 else 0 end )/count(*)as 及格率,
-> sum(case when sc.score>=70 and sc.score<80 then 1 else 0 end )/count(*)as 中等率,
-> sum(case when sc.score>=80 and sc.score<90 then 1 else 0 end )/count(*)as 优良率,
-> sum(case when sc.score>=90 then 1 else 0 end )/count(*)as 优秀率
-> from sc
->group by sc.CId
-> order by count(*) desc, sc.CId asc;
+------+-----------+-----------+-----------+--------------+-----------+-----------+-----------+-----------+
| CId | 最高分 | 最低分 | 平均分 | 选修人数 | 及格率 | 中等率 | 优良率 | 优秀率 |
+------+-----------+-----------+-----------+--------------+-----------+-----------+-----------+-----------+
| 01 | 80.0 | 31.0 | 64.50000 | 6 | 0.6667 | 0.3333 | 0.3333 | 0.0000 |
| 02 | 90.0 | 30.0 | 72.66667 | 6 | 0.8333 | 0.0000 | 0.5000 | 0.1667 |
| 03 | 99.0 | 20.0 | 68.50000 | 6 | 0.6667 | 0.0000 | 0.3333 | 0.3333 |
+------+-----------+-----------+-----------+--------------+-----------+-----------+-----------+-----------+
3 rows in set (0.00 sec)
15.按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺
15.1.按各科成绩进行排序,并显示排名, Score 重复时合并名次
16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺
16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
18.查询各科成绩前三名的记录
19.查询每门课程被选修的学生数
20.查询出只选修两门课程的学生学号和姓名