表架構
Student(S#,Sname,Sage,Ssex) 學生表
Course(C#,Cname,T#) 課程表
SC(S#,C#,score) 成績表
Teacher(T#,Tname) 教師表
建表語句
CREATE TABLE student ( s# INT, sname varchar2(32), sage INT, ssex varchar2(8) ) ; CREATE TABLE course ( c# INT, cname nvarchar(32), t# INT ); CREATE TABLE sc ( s# INT, c# INT, score INT ) ; CREATE TABLE teacher ( t# INT, tname varchar2(16) ) ;
插入測試數據
insert into Student select 1,'劉一',18,'男' FROM DUAL union select 2,'錢二',19,'女' FROM DUAL union select 3,'張三',17,'男' FROM DUAL union select 4,'李四',18,'女' FROM DUAL union select 5,'王五',17,'男' FROM DUAL union select 6,'趙六',19,'女' FROM DUAL; insert into Course select 1,'語文',1 from dual union all select 2,'數學',2 from dual union all select 3,'英語',3 from dual union all select 4,'物理',4 from dual; insert into Teacher select 1,'葉平' from dual union all select 2,'賀高' from dual union all select 3,'楊艷' from dual union all select 4,'周磊' from dual; insert into SC select 1,1,56 from dual union all select 1,2,78 from dual union all select 1,3,67 from dual union all select 1,4,58 from dual union all select 2,1,79 from dual union all select 2,2,81 from dual union all select 2,3,92 from dual union all select 2,4,68 from dual union all select 3,1,91 from dual union all select 3,2,47 from dual union all select 3,3,88 from dual union all select 3,4,56 from dual union all select 4,2,88 from dual union all select 4,3,90 from dual union all select 4,4,93 from dual union all select 5,1,46 from dual union all select 5,3,78 from dual union all select 5,4,53 from dual union all select 6,1,35 from dual union all select 6,2,68 from dual union all select 6,4,71 from dual;
insert into SC select 1,1,56 from dual union all select 1,2,78 from dual union all select 1,3,67 from dual union all select 1,4,58 from dual union all select 2,1,79 from dual union all select 2,2,81 from dual union all select 2,3,92 from dual union all select 2,4,68 from dual union all select 3,1,91 from dual union all select 3,2,47 from dual union all select 3,3,88 from dual union all select 3,4,56 from dual union all select 4,2,88 from dual union all select 4,3,90 from dual union all select 4,4,93 from dual union all select 5,1,46 from dual union all select 5,3,78 from dual union all select 5,4,53 from dual union all select 6,1,35 from dual union all select 6,2,68 from dual union all select 6,4,71 from dual;
問題
1、查詢1課程比2課程成績高的所有學生的學號; select a.s# from (select s#,score from sc where c#=1) a, (select s#,score from sc where c#=2) b where a.s#=b.s# and a.score>b.score; 2、查詢平均成績大於60分的同學的學號和平均成績; select s#,avg(score) from sc having avg(score)>60 group by s# order by s# having 彌補where 不能與 聚合函數連用(即where后面不跟聚合函數) 3、查詢所有同學的學號、姓名、選課數、總成績; select sc.s#,student.sname,count(c#),sum(score) from sc ,student where sc.s# = student.s# group by sc.s#,student.sname order by s#; select Student.S#,Student.Sname,count(SC.C#),sum(score) from Student left Outer join SC on Student.S#=SC.S# group by Student.S#,Sname order by student.s# 4、查詢姓“李”的老師的個數; select count(Tname) from Teacher where Tname like '李%'; 5、查詢沒學過“葉平”老師課的同學的學號、姓名; select Student.S#,Student.Sname from Student where S# not in (select distinct( SC.S#) from SC,Course,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname='葉平'); select s#,sname from STUDENT where S# not in (select STUDENT.s# from STUDENT,SC where STUDENT.S#=SC.S# and SC.c#= (select c# from course where t#=(select t# from TEACHER where tname='葉平'))); 6、查詢學過“001”並且也學過編號“002”課程的同學的學號、姓名; select Student.S#,Student.Sname from Student,SC where Student.S#=SC.S# and SC.C#=1 and exists( select * from sc sc2 WHERE sc2.c#=2 and sc2.s#=SC.s#); 7、查詢學過“葉平”老師所教的所有課的同學的學號、姓名; select STUDENT.s#,STUDENT.SNAME from STUDENT,SC where STUDENT.s#=SC.S# and SC.C#=( select c# from COURSE,TEACHER where COURSE.T#=TEACHER.T# and TEACHER.TNAME='葉平') 8、查詢課程編號2的成績比課程編號1課程低的所有同學的學號、姓名; select a.s#,STUDENT.SNAME from STUDENT, (select s#,score from sc where c#=2) a, (select s#,score from sc where c#=1) b where STUDENT.S#=a.s# and a.s#=b.s# and a.score<b.score ; 9、查詢所有課程成績小於60分的同學的學號、姓名; select STUDENT.s#,sname from sc,STUDENT where SC.S#=STUDENT.s# and score<60 group by STUDENT.s#,sname order by s#; 10、查詢沒有學全所有課的同學的學號、姓名; select s#,sname from STUDENT where s# in (select sc.s# from sc group by SC.s# HAVING count(SC.c#) < (select count(COURSE.c#) from COURSE) ) order by s# 11、查詢至少有一門課與學號為“1001”的同學所學相同的同學的學號和姓名; select SC.s#,STUDENT.sname from sc,STUDENT where SC.s#=STUDENT.s# and c# in (select c# from sc where s#=1) group by SC.s# ,STUDENT.SNAME order by SC.s#
13、把“SC”表中“葉平”老師教的課的成績都更改為此課程的平均成績; update sc set score = (select avg(score) from SC,COURSE,TEACHER where COURSE.t#=TEACHER.t# and sc.c#=COURSE.c# and TNAME='葉平') where c#=(select DISTINCT SC.C# from SC,COURSE,TEACHER where COURSE.t#=TEACHER.t# and sc.c#=COURSE.c# and TNAME='葉平'); 14、查詢和1號的同學學習的課程完全相同的其他同學學號和姓名;select sname from STUDENT where sname like '張%';
select S# from SC where C# in (select C# from SC where S#=1)
group by S# having count(*)=(select count(*) from SC where S#=1);
15、刪除學習“葉平”老師課的SC表記錄;
17、按平均成績從高到低顯示所有學生的“語文”、“數學”、“英語”三門課程成績
select st.s# as 學號,
(select ss.score from sc ss where st.s#=ss.s# and c#=1) as 語文,
(select ss.score from sc ss where st.s#=ss.s# and c#=2) as 數學,
(select ss.score from sc ss where st.s#=ss.s# and c#=3) as 英語,
(select ss.score from sc ss where st.s#=ss.s# and c#=4) as 物理,
avg(ss2.score) as 平均成績
from student st INNER JOIN sc ss2 on st.s#=ss2.s#
group by st.s#
order by avg(ss2.score) desc
18、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分
select c#,max(score),min(score) from sc group by c#;
19、按各科平均成績從低到高和及格率的百分數從高到低順序
SELECT
co.CNAME AS 科目名稱,
avg (ss.score) as 平均成績,
CAST (CAST (SUM(case WHEN COALESCE(ss.score,0)>=60 THEN 1 ELSE 0 END) AS FLOAT)/
COUNT(CO.cname) AS DECIMAL (10, 2)) as 及格率
FROM sc ss ,course co where ss.c# = co.c# GROUP BY co.cname;
SELECT
co.CNAME AS 科目名稱,
avg (ss.score) as 平均成績,
CAST (CAST (SUM(case WHEN COALESCE(ss.score,0)>=60 THEN 1 ELSE 0 END) AS FLOAT)/
COUNT(CO.cname) AS DECIMAL (10, 2)) as 及格率
FROM sc ss INNER JOIN course co
on ss.c# = co.c# GROUP BY co.cname;
24、查詢學生平均成績及其名次
25、查詢各科成績前三名的記錄:(不考慮成績並列情況)
26、查詢每門課程被選修的學生數
select (select cname from course where c#=SC.c#) 課程,count(SC.C#) 學生數 from sc group by SC.c# ;
27、查詢出只選修了一門課程的全部學生的學號和姓名
select SC.S#,SNAME from SC ,STUDENT where SC.S#=STUDENT.S# group by SC.S#,SNAME HAVING count(c#)=1 ;
28、查詢男生、女生人數
select a.男生,b.女生 from
(select count(*) 男生 from STUDENT WHERE SSEX='男') a,
(select count(*) 女生 from STUDENT WHERE SSEX='女') b;
select SSEX,count(SSEX) from STUDENT group by ssex;
29、查詢姓“張”的學生名單
30、查詢同名同性學生名單,並統計同名人數
select sname from STUDENT group by sname HAVING count(*) >1;
32、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列
select c#,avg(score) from sc group by c# order by avg(score) ,c# desc;
33、查詢平均成績大於70的所有學生的學號、姓名和平均成績
select SC.s#, sname, avg(score) from sc,STUDENT where SC.S#=STUDENT.S# group by SC.s#,sname HAVING avg(score)>70
34、查詢課程名稱為“語文”,且分數低於60的學生姓名和分數
select SC.s#,sname,score from sc,COURSE,STUDENT where SC.s#=STUDENT.s# and SC.C#=COURSE.C# and COURSE.cname='語文' and SCORE<60;
35、查詢所有學生的選課情況
select st.sname,co.cname,ss.score
from sc ss,student st,course co where ss.s#=st.s# and ss.c#=co
select st.sname,co.cname,ss.score
from sc ss
inner join student st on ss.s#=st.s#
inner join course co on ss.c#=co.c#
36、查詢任何一門課程成績在70分以上的姓名、課程名稱和分數;
select sname,cname,score from sc
INNER JOIN STUDENT on STUDENT.S#=SC.S#
INNER JOIN COURSE on COURSE.c#=SC.c# and score>70;
40、查詢選修“葉平”老師所授課程的學生中,成績最高的學生姓名及其成績
select STUDENT.SNAME,SC.SCORE from STUDENT,sc where STUDENT.s#=SC.S# and score=
(select max(SC.SCORE) from STUDENT,SC,COURSE,TEACHER where COURSE.T#=TEACHER.T#
and TEACHER.TNAME='葉平' and COURSE.C#=SC.C# )
select st.sname,co.cname,te.tname, ss.score from sc ss
inner join student st on ss.s#=st.s#
inner join course co on ss.c#=co.c#
inner join teacher te on co.t#=te.t#
where te.tname='葉平' order by ss.score desc
41、查詢各個課程及相應的選修人數
select COURSE.CNAME,count(SC.c#) from sc,COURSE where SC.C#=COURSE.C# group by COURSE.CNAME;
43、查詢每門功成績最好的前兩名
44、統計每門課程的學生選修人數,要求輸出課程號和選修人數,查詢結果按人數降序排列,查詢結果按人數降序排列,若人 數相同,按課程號升序排列
select c#,count(c#) from sc group by c# order by count(c#) desc, c# asc;
45、檢索至少選修兩門課程的學生學號
select SC.S# from SC ,STUDENT where SC.S#=STUDENT.S# group by SC.S#,SNAME HAVING count(c#)>=2
46、查詢全部學生都選修的課程的課程號和課程名
SELECT co.c#, co.cname
FROM sc ss
INNER JOIN course co ON co.c# = ss.c#
GROUP BY co.c#, co.cname
HAVING COUNT(co.c#) = (SELECT COUNT (s#) FROM student)
48、查詢兩門以上不及格課程的同學的學號及其平均成績
select s#,count(s#),avg(score) from sc where score<60 group by s# HAVING count(s#) >1