學生表:STUDENT(S#,SNAME,SAGE,SSEX)數據庫
教師表:TEACHER(T#,TNAME)c#
課程表:COURSE(C#,CNAME,T#)code
成績表:SC(S#,C#,SCORE)select
--一、查詢課程編號爲「001」的課程比「002」的課程成績高的全部學生的學號 select A.s# from (select * from A_sc where c# = '001') A inner join (select * from A_sc where c# = '002') B on A.s# = B.s# where A.score > B.score;
--三、查詢全部學生的學號、姓名、選課數、總成績 select A.s#, A.sname, nvl(B.c, 0), nvl(B.s, 0) from A_student A left join (select s#, count(*) c, sum(score) s from A_sc group by s#) B on A.s# = B.s#; SELECT A_STUDENT.S#, A_STUDENT.SNAME, COUNT(A_SC.C#), nvl(SUM(SCORE),0) FROM A_STUDENT LEFT OUTER JOIN A_SC ON A_STUDENT.S#=A_SC.S# GROUP BY A_STUDENT.S#, SNAME;
--五、查詢沒學過「李美玲」老師課的學生的學號、姓名 select s#,sname from A_student D where s# not in (select s# from A_SC A inner join A_course B on A.c# = B.c# inner join A_teacher C on B.t# = C.t# where C.tname = '李美玲');
--六、查詢學過編號爲「001」的課程而且也學過編號爲「002」的課程的學生的學號、姓名 select A.s#, A.sname from A_student A inner join (select s# from A_sc where c# = '001') B on A.s# = B.s# inner join (select s# from A_sc where c# = '002') C on A.s# = C.s#;
--七、查詢學過「李美玲」老師所教的全部課的學生的學號、姓名, select C.s#, D.sname from ( select s#, c# from A_sc where c# in (select c# from A_course A inner join A_teacher B on A.t# = B.t# where B.tname = '李美玲')) C inner join A_student D on C.s# = D.s# group by C.s#, D.sname having count(*) = (select count(*) from A_course A inner join A_teacher B on A.t# = B.t# where B.tname = '李美玲'); --要求 老師名字惟一
--九、查詢全部課程成績小於90分的學生的學號、姓名 select A.s#, A.sname from A_student A inner join (select s#, count(*) c from A_sc group by s#) B on A.s# = B.s# inner join (select s#, count(*) c from A_sc where score < 90 group by s#) C on A.s# = C.s# where B.c = C.c; select s#,sname from A_student where s# not in (select s# from A_sc where score >= 90);
--十、查詢沒有學全全部課的學生的學號、姓名 select s#,sname from A_student where s# not in (select s# from A_sc group by s# having count(*) = (select count(*) from A_course)); select A.s#, A.sname from A_student A inner join A_sc B on A.s# = B.s# group by A.s#,A.sname having count(B.c#) < (select count(*) from A_course);
--十一、查詢至少有一門課與學號爲「2018001」的學生所學課程相同的學生的學號和姓名 select distinct A.s#,sname from A_student A inner join A_sc B on A.s# = B.s# where c# in (select c# from A_sc where s# = '2018001');
--十二、查詢所學課程和學號爲「2018002」的學生全部課程同樣的其餘學生的學號和姓名 select A.s#,sname from A_student A inner join A_sc B on A.s# = B.s# where c# in (select c# from A_sc where s# = '2018002') group by A.s#,sname having count(*) = (select count(*) from A_sc where s# = '2018002');
結果:2018002李四學的課程,張三都學了,且張三有其餘課程,李四沒有學,取的子集,且包含自己。im
--1八、查詢各科成績最高和最低的分 select c#,max(score),min(score) from A_sc group by c# order by c#;
--1九、按各科平均成績從低到高和及格率的百分數從高到低排列,以以下形式顯示:課程號|課程名|平均成績|及格百分數 select A.c#, cname, round(avg(score), 2), round(sum(case when score >= 60 then 1 else 0 end)/count(*) * 100)||'%' from A_course A inner join A_sc B on A.c# = B.c# group by A.c#,cname order by avg(score), sum(case when score >= 60 then 1 else 0 end)/count(*) desc;
--20、查詢以下課程平均成績和及格率的百分數(用1行顯示),其中企業管理爲001,馬克思爲002,UML爲003,數據庫爲004 select round(sum(case c# when '001' then score else 0 end)/sum(case c# when '001' then 1 else 0 end), 2) 企業管理平均分, round(sum(case when c#='001' and score>60 then 1 else 0 end)/sum(case c# when '001' then 1 else 0 end)*100, 2)||'%' 企業管理及格率, sum(case c# when '002' then score else 0 end)/sum(case c# when '002' then 1 else 0 end) 馬克思平均分, sum(case c# when '004' then score else 0 end)/sum(case c# when '004' then 1 else 0 end) 數據庫平均分 from A_sc;
--2一、查詢不一樣老師所教不一樣課程平均分從高到低顯示 select B.c#,B.cname,round(avg(score)),C.t#,C.tname from A_sc A inner join A_course B on A.c# = B.c# inner join A_teacher C on B.t# = C.t# group by B.c#,B.cname,C.t#,C.tname order by avg(score) desc;