Student(Sid,Sname,Sage,Ssex)学生表
Sid:学号
Sname:学生姓名
Sage:学生年龄
Ssex:学生性别
Course(Cid,Cname,Tid)课程表
Cid:课程编号
Cname:课程名称
Tid:教师编号
SC(Sid,Cid,score)成绩表
Sid:学号
Cid:课程编号
score:成绩
Teacher(Tid,Tname)教师表
Tid:教师编号:
Tname:教师名字
1、插入数据
insert into Student (Sid,Sname,Sage,Ssex) values (01,'张三',15,'男');
2、删除课程表所有数据
truncate Course;
3、将学生表中的姓名 张三修改为张大山
SET SQL_SAFE_UPDATES = 0;
update Student set Sname = '张三' where Sname = '张大山'`;
或者
update Student set Sname = '张三' where Sid = '01';
4、查询姓’李’的老师的个数:
select count(Tname) from Teacher where Tname like '李%';
5、查询所有课程成绩小于60的同学的学号、姓名:
select Sid,Sname from Student
where Sid not in
(select Sid from SC where score >60);
6、查询没有学全所有课的同学的学号、姓名
select stu.Sid,stu.Sname,count(SC.Cid) from student stu
left join SC on stu.Sid = SC.Sid
group by stu.Sid,stu.Sname
having count(SC.Cid) < (select count(Cid)from course);
7、查询平均成绩大于60分的同学的学号和平均成绩
select Sid,avg(score) from SC
group by Sid having avg(score) > 60;
8、查询学过“100”并且也学过编号“101”课程的同学的学号、姓名
select st.* from SC a
join SC b on a.sid=b.sid
join Student st on st.Sid=a.Sid
where a.Cid='100' and b.Cid='101' and st.Sid=a.Sid;
9、查询“100”课程比“101”课程成绩高的所有学生的学号
select a.Sid from SC a
join SC b on a.Sid=b.Sid
where a.Cid='100' and b.Cid='101' and a.score > b.score;
10、查询课程编号“100”的成绩比课程编号“101”课程高的所有同学的学号、姓名
select st.Sid,st.Sname from SC a
join SC b on a.Sid=b.Sid
join Student st on st.Sid=a.Sid
where a.Cid='100' and b.Cid='101' and a.score > b.score;
11、查询学过“鲁迅”老师所教的所有课的同学的学号、姓名
select st.sid, st.sname from student st
join SC a on st.sid=a.sid
join course b on a.cid=b.cid
join teacher c on c.tid=b.tid
where c.tname='鲁迅';
12、查询所有同学的学号、姓名、选课数、总成绩
select a.*,st.sname from
(select sid,count(sc.cid),sum(score) from sc group by sid) a, Student st
where st.sid=a.sid;
13、查询至少有一门课与学号为“1”同学所学相同的同学的学号和姓名
select st.sid,st.sname from student st,
select h.sid from
(select * from
(select * from sc) b,
(select * from sc where sc.sid=1) a where b.cid=a.cid) h
group by h.sid
where st.sid=h.sid group up by sid;
14、把“SC”表中“鲁迅”老师教的课的成绩都更改为此课程的平均成绩,
错误
buhui
15、查询和“2”学号的同学学习的课程完全相同的其他同学学号和姓名
16、删除学习“鲁迅”老师课的SC表记录
delete from sc
where sc.cid in
(
select cid from course a,teacher b
where a.tid=b.tid and b.tname='鲁迅'
)
17、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、002号课的平均成绩
18、查询各科成绩最高和最低的分:以如下的形式显示:课程ID,最高分,最低分
select cid,max(score),min(score) from sc group by cid;
19、按各科平均成绩从低到高和及格率的百分数从高到低顺序
select cid,avg(score),sum(case when score>=60 then 1 else 0 end)/count(*)
as '及格率'
from sc group by cid
order by avg(score) , '及格率'desc
20、查询如下课程平均成绩和及格率的百分数(用”1行”显示): 数学(100),语文(101),英语(102)
22、查询不同老师所教不同课程平均分从高到低显示
select max(t.tid),max(t.tname),max(c.cid),max(c.cname),c.cid,avg(score) from sc , course c,teacher t
where sc.cid=c.cid and c.tid=t.tid
group by c.cid
order by avg(score) desc
23、查询如下课程成绩第3名到第6名的学生成绩单:数学(100),语文(101),英语(102)
23、统计下列各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ 小于60]
select sc.cid,c.cname,
sum(case when score between 85 and 100 then 1 else 0 end) as '[100-85]',
sum(case when score between 70 and 80 then 1 else 0 end) as '[85-70]',
sum(case when score between 60 and 70 then 1 else 0 end) as '[70-60]',
sum(case when score <60 then 1 else 0 end) as '[ 小于60]'
from sc,course c
where sc.cid=c.cid
group by sc.cid, c.cname;
24、查询学生平均成绩及其名次
select @rownum:= @rownum+1 AS '名次',st.sname,avg(score) from
student st,sc,(select @rownum:=0) r
where st.sid=sc.sid group by sc.sid order by avg(score);
25、查询各科成绩前三名的记录(不考虑成绩并列情况)
26、查询每门课程被选修的学生数
select sum(sid),cid from sc group by cid;
27、查询出只选修一门课程的全部学生的学号和姓名
select st.sid,st.sname from student st,sc
where st.sid=sc.sid group by st.sid having count(sc.cid)=1;
28、查询男生、女生人数
select ssex,count(*) from sc group by ssex;
29、查询姓“张”的学生名单
select * from student where Sname like '张%';
30、查询同名同姓的学生名单,并统计同名人数
select sname,count(*) from student group by sname having count(*)>1
31、1981年出生的学生名单(注:student表中sage列的类型是datetime)
32、查询平均成绩大于85的所有学生的学号、姓名和平均成绩
select st.sid,st.sname,avg(score) from student st
left join sc on sc.sid=st.sid
group by st.sid having avg(score)>85
33、查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列
select c.cid,c.cname,avg(score) from sc,course c
where sc.cid=c.cid
group by sc.cid order by avg(score),sc.cid;
34、查询课程名称为“英语”,且分数低于60的学生名字和分数
select st.sname,sc.score from student st,sc,course a
where st.sid=sc.sid and sc.cid=a.cid and a.cname='英语'
and sc.score<60;
35、查询所有学生的选课情况
select st.sid,st.sname, c.cname from student st,sc,course c
where st.sid=sc.sid and sc.cid=c.cid;
36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
select st.sname,c.cname,sc.score from student st,course c,sc
where st.sid=sc.sid and c.cid=sc.cid and sc.score>70;
37、查询不及格的课程,并按课程号从大到小的排列
select c.cid,c.cname,sc.score from sc,course c
where sc.cid=c.cid and sc.score<60 order by c.cid desc
38、查询课程编号为“101”且课程成绩在80分以上的学生的学号和姓名
select st.sid,st.sname,sc.score from student st,sc
where cid='101' and st.sid=sc.sid
and score>80
39、求选了课程的学生人数:
select count(*) from student where sid in (
select sid from sc )
select count(distinct sid) from sc;
40、查询选修“鲁迅”老师所授课程的学生中,成绩最高的学生姓名及其成绩
select st.sname,score from student st,sc,course c,teacher t
where
st.sid=sc.sid and sc.cid=c.cid and c.tid=t.tid
and t.tname='鲁迅' and sc.score=
(select max(score) from sc where sc.cid=c.cid)
select st.sname,score from student st,sc,course c,teacher t
where
st.sid=sc.sid and c.cid=sc.cid and t.tid=c.tid
and t.tname='鲁迅' and sc.score=
(select max(score) from sc where sc.cid=c.cid);
41、检索至少选修两门课程的学生学号
select sid from sc group by sid having count(cid)>=2
select sid from sc group by sid having count(sid)>=2
42、查询全部学生都选修的课程的课程号和课程名(1.一个课程被全部的学生选修,2.所有的学生选择的所有课程)
select cid,cname from course
where cid in
(select cid from sc group by cid)
43、查询没学过“鲁迅”老师讲授的任一门课程的学生姓名
select sname from student
where sid not in
(select sc.sid from sc,course c, teacher t
where sc.cid=c.cid and t.tname='鲁迅'
and t.tid=c.tid
)
44、查询两门以上不及格课程的同学的学号及其平均成绩
select sid, avg(score) from sc where sid in
(select sid from sc where score<60
group by sid having count(sid)>1)
group by sid;
45、检索“101”课程分数小于60,按分数降序排列的同学学号
select sid, score from sc
where cid='101' having score<60
order by score desc;
46、删除“2”同学的“101”课程的成绩
SET SQL_SAFE_UPDATES = 0;
delete from sc where sid='2' and cid='101';