--建表(用于sql练习) create database practice; use practice;
Create table student(sid int primary key auto_increment,sname varchar(11),sage int ,ssex varchar(1)) character set utf8 collate utf8_general_ci;
Create table course(cid int primary key auto_increment,cname varchar(11),tid int) character set utf8 collate utf8_general_ci;
Create table sc(sid int , cid int ,score int) character set utf8 collate utf8_general_ci;
Create table teacher(tid int primary key auto_increment, tname varchar(11)) character set utf8 collate utf8_general_ci;
--插入数据
Insert into student (sname,sage,ssex) values ('小宝',23,'男');
insert into student (sname,sage,ssex) values ('小虎',16,'男');
insert into student (sname,sage,ssex) values ('小熊',15,'女');
insert into course (cname,tid) values('语文',1);
insert into course (cname,tid) values('数学',2);
insert into course (cname,tid) values('英语',3);
insert into sc (sid,cid,score) values (1,1,33);
insert into sc (sid,cid,score) values (1,2,44);
insert into sc (sid,cid,score) values (1,3,66);
insert into sc (sid,cid,score) values (2,1,76);
insert into sc (sid,cid,score) values (2,2,99);
insert into sc (sid,cid,score) values (2,3,65);
insert into sc (sid,cid,score) values (3,1,83);
insert into sc (sid,cid,score) values (3,2,82);
insert into sc (sid,cid,score) values (3,3,98);
insert into teacher ( tname) value ('沈腾');
insert into teacher ( tname) value ('沙溢');
insert into teacher ( tname) value ('成龙');
--修改数据
update sc set score=76 where sid=2 and cid=1 ;
delete from sc where sid=2 and cid =3;
--查询
select * from student; select * from course; select * from sc;select * from teacher;
1、查询“1”课程比“2”课程成绩高的所有学生的学号(sid);
select a.sid from
(select sid,score from sc where cid=1)a,
(select sid,score from sc where cid=2)b
where a.score>b.score and a.sid=b.sid
;
2、查询平均成绩大于60分的同学的学号和平均成绩;
select sid,avg(score) from sc group by sid having avg(score)>60;
3、查询所有同学的学号、姓名、选课数、总成绩;
select a.sid,count(b.cid),sum(b.score) from
(select sid,sname from student )a,
(select sid,cid,score from sc )b
where a.sid=b.sid
group by a.sid;
4、查询姓“沙”的老师的个数;
select count(1) from teacher where tname like '沙%';
5、查询没学过“沈腾”老师课的同学的学号、姓名;
select sid,sname from student where sid not in
( select distinct sc.sid from sc,teacher,course where sc.cid=course.cid
and teacher.tid=course.tid and teacher.tname='沈腾'
);
6、查询学过“1”并且也学过编号“2”课程的同学的学号、姓名;
select sid,sname from student where sid in
(select distinct a.sid from
(select sid,score from sc where cid=1)a,
(select sid,score from sc where cid=2)b
where a.sid=b.sid
);
7、查询学过“沈腾”老师所教的所有课的同学的学号、姓名;
select sid,sname from student where sid in
( select distinct sc.sid from sc,teacher,course where sc.cid=course.cid
and teacher.tid=course.tid and teacher.tname='沈腾'
);
8、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;
select sid,sname from student where sid in
(select distinct a.sid from
(select sid,score from sc where cid=1)a,
(select sid,score from sc where cid=2)b
where a.sid=b.sid and b.score < a.score
);
9、查询所有课程成绩小于60分的同学的学号、姓名;
select sid,sname from student where sid in
(select a.sid from
(select sid ,count(1) as num from sc where score < 60 group by sid ) a,
(select sid ,count(1) as num from sc group by sid ) b
where a.sid=b.sid and a.num=b.num
);
10、查询没有学全所有课的同学的学号、姓名;
select sid,sname from student where sid in (
select sid from sc group by sid having count(cid)<(select count(*) from course )
);
11、查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名;
select sid,sname from student where sid in
(select sid from sc where cid in (select cid from sc where sid=1 )
and sid <>1 );
12、查询至少学过学号为“1”同学所有课的其他同学学号和姓名;
select sid,sname from student where sid in
( select b.sid from
(select sid,cid from sc where sid=1)a,
(select sid,cid from sc where sid<>1)b
where a.cid=b.cid
group by b.sid having count(1)>=(select count(cid) from sc where sid=1)
);
13、把“SC”表中“沈腾”老师教的课的成绩都更改为此课程的平均成绩;
set @var1=(select avg(score) from sc where cid =
(select cid from course c ,teacher t where t.tname='沈腾' and c.tid=t.tid)
group by cid);
update sc set score = @var1
where
cid=(select cid from course c ,teacher t where t.tname='沈腾' and c.tid=t.tid)
;
14、查询和“2”号的同学学习的课程完全相同的其他同学学号和姓名;
select sid,sname from student where sid in
( select b.sid from
(select sid,cid from sc where sid=2)a,
(select sid,cid from sc where sid<>2)b
where a.cid=b.cid
group by b.sid having count(1)=(select count(cid) from sc where sid=2)
);
15、删除学习“沈腾”老师课的SC表记录;
delete from sc where cid in
(select c.cid from course c,teacher t where t.tname='沈腾' and c.tid=t.tid);
16、向SC表中插入一些记录,这些记录要求符合以下条件:
没有上过编号“3”课程的同学学号、2号课的平均成绩;
insert into sc
select sid,cid,(select avg(score) from sc where cid=2 group by cid) from sc
where sid not in (select sid from sc where cid=3)
;
17、按平均成绩从高到低显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,
按如下形式显示: 学生ID,语文,数学,英语,课程数,平均分
select sid as '学生ID',
(select score from sc where sid=t.sid and cid=(select cid from course where cname='语文')) as '语文' ,
(select score from sc where sid=t.sid and cid=(select cid from course where cname='数学')) as '数学' ,
(select score from sc where sid=t.sid and cid=(select cid from course where cname='英语')) as '英语' ,
count(cid) as '课程数',
avg(score) as '平均分'
from sc as t group by sid order by avg(score) desc;
18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
select cid,max(score),min(score) from sc group by cid ;
19、按各科平均成绩从低到高和及格率的百分数从高到低顺序
select cid,avg(score) sc_avg,
(select ROUND(count(if(score>=60,1,null))/count(1)*100,1)
from sc where cid=t.cid group by cid ) as rate
from sc as t group by cid order by sc_avg asc ,rate desc ;
20、查询如下课程平均成绩和及格率的百分数(用"1行"显示,课程id可以直接使用):1语文2数学3英语
select 1,2,3,
(select avg(score) from sc where cid=1 group by cid ) as avg1,
(select avg(score) from sc where cid=2 group by cid ) as avg2,
(select avg(score) from sc where cid=3 group by cid ) as avg3,
(select ROUND(count(if(score>=60,1,null))/count(1)*100,1)
from sc where cid=1 group by cid ) as rate1,
(select ROUND(count(if(score>=60,1,null))/count(1)*100,1)
from sc where cid=2 group by cid ) as rate2,
(select ROUND(count(if(score>=60,1,null))/count(1)*100,1)
from sc where cid=3 group by cid ) as rate3
from sc limit 1;
21、查询不同老师所教不同课程平均分从高到低显示
教师id,教师姓名,课程id,课程名称,平均成绩
select b.tid,b.tname,a.cid,b.cname,a.a_sc from
(select cid, avg(score) as a_sc from sc
group by cid order by a_sc desc
) a join
(select cid,cname,t.tid,tname from teacher t,course c where t.tid=c.tid )b
on a.cid = b.cid
order by a_sc desc ;
22、查询平均成绩第2名到第3名的学生成绩单:语文(1),数学(2),英语(3)
[学生ID],[学生姓名],语文,数学,英语,平均成绩
set @i=0;
select * from (
select *,(@i :=@i + 1) AS num from (
select s.sid,s.sname,
(select score from sc where sid=s.sid and cid=1 ) as '语文',
(select score from sc where sid=s.sid and cid=2 ) as '数学',
(select score from sc where sid=s.sid and cid=3 ) as '英语',
avg(sc.score) as avs
from student s , sc
where s.sid=sc.sid
group by s.sid
order by avs
)aa )bb where num>=2 and num<=3;
23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
select s.cid,c.cname,
(select count(*) from sc where cid=s.cid and score >=85 and score <=100) as A,
(select count(*) from sc where cid=s.cid and score >=70 and score <85) as B,
(select count(*) from sc where cid=s.cid and score >=60 and score <70) as C,
(select count(*) from sc where cid=s.cid and score <60) as D
from sc s, course c
where s.cid = c.cid
group by s.cid ;
24、查询学生平均成绩及其名次
set @i=0;
select *,(@i:=@i+1) as num from (
select sid,avg(score) avs from sc group by sid order by avs )a;
25、查询各科成绩前两名的记录:(不考虑成绩并列情况)
SELECT a.* FROM sc a
WHERE (
SELECT COUNT(1) FROM sc b WHERE a.`cid` = b.`cid`
AND b.`score` > a.score
) < 2
ORDER BY cid, score DESC;
26、查询每门课程被选修的学生数
select cid,count(*) num from sc group by cid ;
27、查询出只选修了一门课程的全部学生的学号和姓名
select sid,sname from student where sid in
(select sid from
(select sid,count(*) num from sc group by sid having num=1 ) a
);
28、查询男生、女生人数
select ssex ,count(*) from student group by ssex ;
29、查询姓“小”的学生名单
select * from student where sname like '小%';
30、查询同姓学生名单,并统计人数
select sname ,count(*) as num from student group by sname having num>1 ;
31、1981年出生的学生名单(注:Student表中Sage列的类型是datetime)
主要考察日期函数:date_format(now(), '%Y%m%d%H%i%s')
32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
select cid,avg(score) asv
from sc
group by cid
order by asv,cid desc;
33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩
select sid,sname,
(select avg(score) from sc where sid = s.sid ) as asv
from student s ;
34、查询课程名称为“语文”,且分数低于60的学生姓名和分数
select * from (
select sname,(select score from sc where sid = s.sid and score<60) as score
from student s )a where a.score is not null ;
35、查询所有学生的选课情况; 学号,姓名,课程数组
select sid,sname,
(select group_concat(a.cname) as courses
from course a, sc b where a.cid=b.cid and b.sid=a.sid )
from student a;
36、查询课程成绩在70分以上的姓名、课程名称和分数;
select a.sid,a.sname,b.cid,c.cname,b.score
from student a,sc b,course c
where a.sid=b.sid and b.cid=c.cid and b.score>=70;
37、查询不及格的课程,并按课程号从大到小排列
select cid,score from sc where score<60 order by cid desc;
38、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;
select sid,sname from student where sid in
(select sid from sc where score >80);
39、求选了课程的学生人数
select cid,count(1) from sc group by cid;
40、查询选修“沈腾”老师所授课程的学生中,成绩最高的学生姓名及其成绩
select a.sname,b.score
from student a,sc b,course c ,teacher t
where a.sid=b.sid and b.cid=c.cid and c.tid=t.tid and t.tname='沈腾'
and b.score=( select max(score) from SC where cid=b.cid )
41、查询各个课程及相应的选修人数(一个成绩肯定对应一个人)
select cid ,count(1) from sc group by cid;
42、查询不同课程成绩相同的学生的学号、课程号、学生成绩
select sid,cid,score from sc a
where score in
(select score from sc group by score having count(1)>1 );
43、查询每门课程前两名(考虑成绩并列情况)
44、统计每门课程的学生选修人数(超过1人的课程才统计)。
要求输出课程号和选修人数,查询结果按人数降序排列,
查询结果按人数降序排列,若人数相同,按课程号升序排列
select cid,count(1) as num from sc
group by cid having num>1 order by num desc,cid
45、检索至少选修两门课程的学生学号
select sid from sc group by sid having count(1)>1;
46、查询全部学生都选修的课程的课程号和课程名
select cid,cname from course where cid in
(select cid from sc group by cid having count(1)=
(select count(*) from student));
47、查询没学过“沈腾”老师讲授的任一门课程的学生姓名
select sid,sname from student where sid not in
(select sid from sc a,course b,teacher c
where a.cid=b.cid and b.tid=c.tid and c.tname='沈腾') ;
48、查询1门以上不及格课程的同学的学号及其平均成绩
select sid,avg(score) asv from sc where sid in
(select sid from sc where score<60 group by sid having count(1)>1 )
group by sid ;
49、检索“3”课程分数小于60,按分数降序排列的同学学号
select sid from sc where score<60 and cid=3 order by score desc;
50、删除“2”同学的“1”课程的成绩
delete from sc where sid=2 and cid =1;