学生表
CREATE TABLE student( sno VARCHAR(10) PRIMARY KEY, sname VARCHAR(20), sage NUMERIC(2), ssex VARCHAR(5) );
老师表 CREATE TABLE teacher( tno VARCHAR(10) PRIMARY KEY, tname VARCHAR(20) );
课程表 CREATE TABLE course( cno VARCHAR(10), cname VARCHAR(20), tno VARCHAR(20), CONSTRAINT pk_course PRIMARY KEY (cno,tno) );
分数表 CREATE TABLE sc( sno VARCHAR(10), cno VARCHAR(10), score NUMERIC(4,2), CONSTRAINT pk_sc PRIMARY KEY (sno,cno) ); /*******初始化学生表的数据******/ insert into student values ('s001','张三',23,'男'); insert into student values ('s002','李四',23,'男'); insert into student values ('s003','吴鹏',25,'男'); insert into student values ('s004','琴沁',20,'女'); insert into student values ('s005','王丽',20,'女'); insert into student values ('s006','李波',21,'男'); insert into student values ('s007','刘玉',21,'男'); insert into student values ('s008','萧蓉',21,'女'); insert into student values ('s009','陈萧晓',23,'女'); insert into student values ('s010','陈美',22,'女'); commit; /******************初始化教师表***********************/ insert into teacher values ('t001', '刘阳'); insert into teacher values ('t002', '谌燕'); insert into teacher values ('t003', '胡明星'); commit; /***************初始化课程表****************************/ insert into course values ('c001','J2SE','t002'); insert into course values ('c002','Java Web','t002'); insert into course values ('c003','SSH','t001'); insert into course values ('c004','Oracle','t001'); insert into course values ('c005','SQL SERVER 2005','t003'); insert into course values ('c006','C#','t003'); insert into course values ('c007','JavaScript','t002'); insert into course values ('c008','DIV+CSS','t001'); insert into course values ('c009','PHP','t003'); insert into course values ('c010','EJB3.0','t002'); commit; /***************初始化成绩表***********************/ insert into sc values ('s001','c001',78.9); insert into sc values ('s002','c001',80.9); insert into sc values ('s003','c001',81.9); insert into sc values ('s004','c001',60.9); insert into sc values ('s001','c002',82.9); insert into sc values ('s002','c002',72.9); insert into sc values ('s003','c002',81.9); insert into sc values ('s001','c003','59'); commit;
1、查询“c001”课程比“c002”课程成绩高的所有学生的学号; select c.*,d.* from (select*from sc a where a.cno = 'c001') c, (select*from sc a where a.cno = 'c002') d where c.score < d.score and c.sno = d.sno; 2、查询平均成绩大于60 分的同学的学号和平均成绩; select avg(score),sno from sc group by sno having avg(score)>60; 3、查询所有同学的学号、姓名、选课数、总成绩; select st.sno , st.sname , a.sum_score 总成绩,a.cno_count 选课数 from student st, (select sum(score) sum_score, count(cno) cno_count,sno from sc group by sno) a where a.sno = st.sno; 4、查询姓“刘”的老师的个数 SELECT COUNT(tname) FROM teacher where tname like '刘%'; 5、查询没学过“谌燕”老师课的同学的学号、姓名 select*from student select * from student st where st.sno not in (select distinct sno from sc s join course c on s.cno=c.cno join teacher t on c.tno=t.tno where tname='谌燕'); 6、查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名 select st.* from sc a join sc b on a.sno = b.sno join student st on st.sno = a.sno where a.cno = 'c001' and b.cno = 'c002' and st.sno = a.sno 7、查询学过“谌燕”老师所教的课的同学的学号、姓名; select * from student st where st.sno in (select distinct sno from sc s join course c on s.cno=c.cno join teacher t on c.tno=t.tno where tname='谌燕'); 8、查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名; select * from student st join sc a on st.sno=a.sno join sc b on st.sno=b.sno where a.cno='c002' and b.cno='c001' and a.score < b.score
9、查询所有课程成绩小于60 分的同学的学号、姓名; select st.* from student st join sc s on st.sno = s.sno join course c on s.cno = c.cno where s.score < 60; 10、查询至少有一门课与学号为“s001”的同学所学相同的同学的学号和姓名; select st.* from student st, (select distinct a.sno from (select*from sc) a, (select*from sc where sc.sno = 's001') b where a.cno = b.cno) h where st.sno = h.sno and st.sno<>'s001'; 11、把“SC”表中“谌燕”老师教的课的成绩都更改为此课程的平均成绩; update sc sc1 set sc1.score = (select avg(sc2.score) from teacher t1, course c1, sc sc2 where c1.tno = t1.tno and sc2.cno = c1.cno and sc2.cno = sc1.cno and t1.tname = '谌燕' group by c1.cno) where sc1.cno = (select c1.cno from teacher t1, course c1 where c1.tno = t1.tno and sc1.cno = c1.cno and t1.tname = '谌燕') ;
12、查询和“s001”号的同学学习的课程完全相同的其他同学学号和姓名;
select* from sc where sno<>'s001' minus ( select* from sc minus select * from sc where sno='s001' ) 13、删除学习“谌燕”老师课的SC 表记录; 14、向SC 表中插入一些记录,这些记录要求符合以下条件:没有上过编号“c002”课程的同学学号、“c002”号课的平均成绩
15、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分 select sc1.sno 课程 , max(sc1.score)最高分 , min(sc1.score) 最低分 from sc sc1 group by sc1.sno select sc1.cno 课程 ,max(sc1.score) 最高分, min(sc1.score) 最低分 from sc sc1 group by sc1.cno;
16、按各科平均成绩从低到高和及格率的百分数从高到低顺序 select sc1.sno, avg(sc1.score) from sc sc1 group by sc1.sno order by avg(sc1.score) 17、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60] select sc1.cno,c.cname, sum(case when 85<sc1.score and sc1.score<= 100 then 1 else 0 end) 优秀, sum(case when 70<sc1.score and sc1.score<= 85 then 1 else 0 end) 优良, sum(case when 60<sc1.score and sc1.score<= 70 then 1 else 0 end) 中等, sum(case when 0<sc1.score and sc1.score<= 60 then 1 else 0 end) 不及格 from sc sc1 , course c where sc1.cno = c.cno group by sc1.cno ,c.cname; 18、查询各科成绩前三名的记录:(不考虑成绩并列情况) select*from sc a where (SELECT COUNT(*) FROM sc b where a.cno = b.cno and a.score < b.score)<3 order by a.cno,a.score desc 19、查询每门课程被选修的学生数 select sc1.cno ,count(sc1.sno)from sc sc1 group by sc1.cno 20、查询出只选修了一门课程的全部学生的学号和姓名 select st.sno,st.sname,count(sc1.cno) from student st left join sc sc1 on st.sno = sc1.sno group by st.sno,st.sname having count(sc1.cno) = 1; 21、查询男生、女生人数 select ssex, count(sno) from student group by ssex; 22、查询姓“张”的学生名单 select*from student where sname like'张%'; 23、查询同名同性学生名单,并统计同名人数 select count(sno),sname from student group by sname having count(sno)>1
24、1997 年出生的学生名单(注:Student 表中Sage 列的类型是number) select*from student where sage = (2020-1997) 25、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列 select avg(sc1.score),sc1.cno from sc sc1 group by sc1.cno order by avg(sc1.score) asc,cno desc; 26、查询平均成绩大于85 的所有学生的学号、姓名和平均成绩
select st.sno,st.sname,avg(sc1.score) from student st, sc sc1 where st.sno = sc1.sno(+) group by st.sno ,st.sname having avg(sc1.score) >85 27、查询课程名称为“数据库”,且分数低于60 的学生姓名和分数 select st.sname,sc1.score from student st, sc sc1,course co where co.cname= 'Java Web' and co.cno = sc1.cno and sc1.sno = st.sno and sc1.score <60 28、查询所有学生的选课情况; select st.sname,co.cname from student st, sc sc1,course co where co.cno = sc1.cno and sc1.sno = st.sno 29、查询任何一门课程成绩在70 分以上的姓名、课程名称和分数; select st.sname,co.cname,sc1.score from student st, sc sc1,course co where co.cno = sc1.cno and sc1.sno = st.sno and sc1.score >70 30、查询不及格的课程,并按课程号从大到小排列 select sc.sno,c.cname,sc.score from sc,course c where sc.cno=c.cno and sc.score<60 order by sc.cno desc; 31、查询课程编号为c001 且课程成绩在80 分以上的学生的学号和姓名; select st.sname,co.cname,sc1.score from student st, sc sc1,course co where co.cno = sc1.cno and sc1.sno = st.sno and sc1.score >80 and sc1.cno = 'c001'; 32、求选了课程的学生人数 SELECT COUNT(distinct cno) FROM sc 33、查询选修“谌燕”老师所授课程的学生中,成绩最高的学生姓名及其成绩 select st.sname,co.cname, max(sc1.score),sc1.cno,th.tname from student st, sc sc1,course co,teacher th where co.cno = sc1.cno and sc1.sno = st.sno and th.tno = co.tno and th.tname = '谌燕' group by st.sname,co.cname ,sc1.cno,th.tname;
34、查询各个课程及相应的选修人 select sc1.cno,co.cname,count(sno) from sc sc1 , course co where sc1.cno = co.cno group by sc1.cno,co.cname 35、查询不同课程成绩相同的学生的学号、课程号、学生成绩 select sc1.* from sc sc1, sc sc2 where sc1.score = sc2.score and sc1.cno <> sc2.cno; 36、查询每门功课成绩最好的前两名 select * from ( select sno,cno,score,row_number()over(partition by cno order by score desc) my_rn from sc t ) where my_rn<=2