1、查詢“c001”課程比“c002”課程成績高的所有學生的學號;
--方法一:
select m.* from
(select * from sc a where a.cno='c001') m,--分組課程
(select * from sc b where b.cno='c002') n
where m.sno = n.sno and m.score > n.score;--學號一樣,課程成績比較
--方法二:
select * from sc a
where a.cno='c001'
and exists(select * from sc b where b.cno='c002' and a.score>b.score and a.sno = b.sno);
--在學號一樣時,存在一個b的課程成績小於a
2、查詢平均成績大於60 分的同學的學號和平均成績;
select c.sno "學號",avg(c.score) "平均成績" from sc c group by c.sno having avg(c.score) > 60;
3、查詢所有同學的學號、姓名、選課數、總成績;
select * from sc;
select *from student;
select a.*,s.sname from (select sno,sum(score),count(cno) from sc group by sno) a ,student s where a.sno=s.sno;
--在兩表中學號一樣的進行學號、總成績、選課數分組
4、查詢姓“劉”的老師的個數;
select count(*) "姓 劉 的老師個數" from teacher t where t.tname like '劉%';
5、查詢沒學過“諶燕”老師課的同學的學號、姓名;
--放法一:
select s.sno "學號",s.sname "姓名" from student s
where s.sno
not in
(select distinct s.sno --去掉重復學號
from sc s,
(select c.*
from course c ,-- c總課程
(select tno
from teacher t
where tname='諶燕')t -- t是 諶燕 老師的課程
where c.tno=t.tno) b -- b是總課程與諶燕老師交集
where s.cno = b.cno ) --條件 課程一樣
select * from teacher ;
select * from course ;
--方法二:
select st.sno "學號",st.sname "姓名" from student st where st.sno not
in(select distinct s.sno --去學號重復的
from sc s join course c on s.cno=c.cno join teacher t on c.tno=t.tno
--要求表sc和表course的課程一樣,表course和表teacher的課程編號一樣
and tname='諶燕') --也可以用 where
6、查詢學過“c001”並且也學過編號“c002”課程的同學的學號、姓名;
select st.sno "學號",st.sname "姓名" from sc s
join sc a on s.sno = a.sno --學號是一樣的
join student st
on s.sno = a.sno
where s.cno = 'c001' and a.cno = 'c002' and st.sno=s.sno;
7、查詢學過“諶燕”老師所教的所有課的同學的學號、姓名;
select st.sno"學號",st.sname "姓名",s.cno "課程" from student st
join sc s on st.sno=s.sno --學號一樣
join course c on s.cno=c.cno --再課程一樣
join teacher t on c.tno=t.tno --課程編號一樣
where t.tname='諶燕';
8、查詢課程編號“c002”的成績比課程編號“c001”課程低的所有同學的學號、姓名;
select /*st.sno "學號",st.sname "姓名"*/* from student st
join sc s1 on st.sno = s1.sno
join sc s2 on st.sno = s2.sno
where s1.cno ='c001' and s2.cno = 'c002' and s1.score > s2.score;
9、查詢所有課程成績小於60 分的同學的學號、姓名;
select /*st.sno "學號",st.sname "姓名"*/* from student st
join sc s on st.sno = s.sno
/*join course c on s.cno=c.cno*/--也可以加上
where s.score < 60;
10、查詢沒有學全所有課的同學的學號、姓名;
select stu.sno,stu.sname,count(sc.cno) from
student stu left join sc on stu.sno=sc.sno --student和sc左連接
group by stu.sno,stu.sname
having count(sc.cno)<(select count(distinct cno)from course)
11、查詢至少有一門課與學號為“s001”的同學所學相同的同學的學號和姓名;
select st.* from student st,
(select distinct a.sno from --去掉重復學號
(select * from sc) a,
(select * from sc where sc.sno='s001') b --sc表中學號為s001的作為一個表對象
where a.cno=b.cno) h --a、b表 課程一樣
where st.sno=h.sno and st.sno<>'s001';--學號不為s001的
12、查詢至少學過學號為“s001”同學所有一門課的其他同學學號和姓名;
select * from sc
left join student st --左連接
on st.sno=sc.sno --學號一樣
where sc.sno<>'s001' --學號不為s001的
and sc.cno in(select cno from sc where sno='s001'); --查詢學號為s001的所有課程信息
13、把“SC”表中“諶燕”老師教的課的成績都更改為此課程的平均成績;
update sc c set score=(select avg(c.score) from course a,teacher b
where a.tno=b.tno
and b.tname='諶燕'
and a.cno=c.cno
group by c.cno) --課程分組
where cno in( --限定條件
select cno from course a,teacher b
where a.tno=b.tno
and b.tname='諶燕'); --課程中所教老師為 諶燕
14、查詢和“s002”號的同學學習的課程完全相同的其他同學學號和姓名;
select sc.sno,st.sname from student st,sc
where st.sno=sc.sno
group by sc.sno,st.sname --以學號、姓名分組
having count(*)=(select count(*) from sc where sno='s002' group by sno) --與s002課程數量一樣的
and sc.sno!='s002'; --除學號為s002的其他同學
15、刪除學習“諶燕”老師課的SC 表記錄;
deleted from sc
where sc.cno --課程限定
in( select cno from course c
left join teacher t on c.tno=t.tno --左連接,教師編號
where t.tname='諶燕');
select * from teacher;
16、向SC 表中插入一些記錄,這些記錄要求符合以下條件:沒有上過編號“c002”課程的同學學號、“c002”號課的平均成績;
select * from sc;
savepoint B; --回滾點B
insert into sc (sno,cno,score) --學號,課程編號,成績
select distinct st.sno,sc.cno,
(select avg(score) from sc where cno='c002') --去重,加上學號、課程編碼、c002的平均成績
from student st,sc
where not exists --不存在
(select * from sc where cno='c002' and sc.sno=st.sno) and sc.cno='c002';
savepoint A; --回滾點A
rollback B; --回滾到B
17、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分
select s.cno,max(s.score),min(s.score) from sc s group by s.cno;--在表sc中以課程分組
18、按各科平均成績從低到高和及格率的百分數從高到低順序
select cno "課程編號",avg(score) "課程平均成績",sum(case when score>=60 then 1 else 0 end)/count(*) "課程及格率"
from sc group by cno
order by avg(score) , /*sum(case when score>=60 then 1 else 0 end)/count(*)*/課程及格率 desc
19、查詢不同老師所教不同課程平均分從高到低顯示
select max(t.tno),max(t.tname),max(c.cno),max(c.cname),avg(score) --最大教師編號、教師名字、課程編號、課程名字、課程平均分
from sc,course c,teacher t --三個主要表
where sc.cno = c.cno and c.tno = t.tno --課程編號、教師編號一樣
group by c.cno --課程編號分組
order by avg(score) desc; --以課程平均分從高到低排序
20、統計列印各科成績,各分數段人數:課程ID,課程名稱,[100-85],[85-70],[70-60],[ <60]
select sc.cno "課程ID",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 85 then 1 else 0 end) AS "[85-70]",
sum(case when score >= 60 and score < 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.cno=c.cno
group by sc.cno ,c.cname;
21、查詢各科成績前三名的記錄:(不考慮成績並列情況)
/*select * from
(select sno,cno,score,row_number()over(partition by cno order by score desc) rn from sc)
where rn<4;*/
22、查詢每門課程被選修的學生數
select s.cno "課程編號",count(s.cno) "選修人數" from sc s group by s.cno;--分組統計
select * from course;
select * from sc;
23、查詢出只選修了一門課程的全部學生的學號和姓名
select s.sno "學號",st.sname "姓名" from sc s
join student st on s.sno = st.sno --學號一樣
group by st.sname,s.sno --以姓名、學號分組
having count(cno)=1; --統計課程編碼為1的
24、查詢男生、女生人數
select * from student;
select st.ssex "性別",count(*) "人數" from student st group by st.ssex;
25、查詢姓“張”的學生名單
select * from student st where st.sname like '張%';
26、查詢同名同性學生名單,並統計同名人數
select st.sname,count(*) from student st
group by st.sname; --姓名分組,統計姓名個數
select st.sname,count(*) from student st
group by st.sname having count(*)>1; --姓名分組,統計同名個數
27、1995 年出生的學生名單(注:Student 表中Sage 列的類型是number)
select sno,sname,sage,ssex from student t
where to_char(sysdate,'yyyy')-sage = 1995;--將日期型轉為字符型,相減
28、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列
select sc.cno "課程ID", avg(sc.score) "課程平均成績" from sc
group by sc.cno --按課程號分組
order by avg(sc.score) asc, sc.cno desc; 先按課程平均成績升序,再按課程號降序
29、查詢平均成績大於75 的所有學生的學號、姓名和平均成績
select sc.sno "學號",st.sname "姓名",avg(sc.score) "平均成績" from sc
join student st on st.sno = sc.sno
group by sc.sno,st.sname --分組要與上面的 學號、姓名 對應
having avg(sc.score) > 75;
30、查詢課程名稱為“SSH”,且分數低於60 的學生姓名和分數
select * from course;
select * from sc;
select * from student;
select st.sname,sc.score
from sc,student st,course c
where st.sno=sc.sno and sc.cno=c.cno and c.cname='SSH' and sc.score < 60;--相同項作等
31、查詢所有學生的選課情況;
select sc.sno "學號",st.sname "姓名",sc.cno "課程ID",c.cname "課程名稱",t.tname "授課教師"
from student st, course c,sc,teacher t
where st.sno=sc.sno and sc.cno=c.cno and c.tno = t.tno;
32、查詢任何一門課程成績在70 分以上的姓名、課程名稱和分數;
select st.sname "姓名",c.cname "課程名稱",sc.score "分數"
from sc,course c,student st
where sc.sno = st.sno and c.cno = sc.cno and sc.score > 70;
33、查詢小於75的課程,並按課程號從大到小排列
select c.cno "課程號", c.cname "課程名稱",sc.score "課程分數" from sc,course c
where sc.cno = c.cno and sc.score < 75
order by c.cno desc;--按課程號降序
34、查詢課程編號為c001 且課程成績在80 分以上的學生的學號和姓名;
select st.sno "學號",st.sname "姓名",sc.score "分數"
from sc,student st --只需要sc和student表
where sc.sno = st.sno and sc.cno = 'c001' and sc.score > 80;
35、求選了課程的學生人數
select count(distinct sc.sno) from sc; --只需統計sc表中學號不重復的個數
36、查詢選修“諶燕”老師所授課程的學生中,成績最高的學生姓名及其成績
select st.sname "學生姓名",sc.score "分數"
from sc,student st,course c,teacher t
where sc.sno = st.sno and c.tno = t.tno and sc.cno = c.cno and t.tname = '諶燕'
and sc.score = (select max(sc.score) from sc where sc.cno = c.cno );
--這里后面必須要用sc.cno = c.cno區別每一個課程
37、查詢各個課程及相應的選修人數
select c.cno,count(sc.cno) from course c,sc
where sc.cno = c.cno
group by c.cno;--與22題一樣,這里用兩個表,需要課程名一樣
38、查詢不同課程成績相同的學生的學號、課程號、學生成績
select /*a.sno "學號",a.cno "課程號",a.score "學生成績" */ a.*
from sc a ,sc b
where a.score = b.score and a.cno<>b.cno; --成績一樣、課程不一樣
39、查詢每門功課成績最好的前兩名
/*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;*/
40、統計每門課程的學生選修人數(超過10 人的課程才統計)。要求輸出課程號和選修人數,
查詢結果按人數降序排列,若人數相同,按課程號升序排列
select cno,count(sno) from sc group by cno
having count(sno)>10 --超過10人的課程
order by count(sno) desc,cno asc; --先按人數降序,再按課程降序
41、檢索至少選修兩門課程的學生學號
select sc.sno from sc
group by sno having count(cno) > 1;/* having count(sno)>1*/
42、查詢全部學生都選修的課程的課程號和課程名
select distinct(c.cno),c.cname from sc,course c
where sc.cno = c.cno;
||
select cno,cname from course c
where c.cno in(select cno from sc group by cno)
43、查詢沒學過“諶燕”老師講授的任一門課程的學生姓名
select st.sname from student st
where st.sno not in
(select distinct sc.sno from sc,course c,teacher t --學號去重
where sc.cno=c.cno and c.tno=t.tno and t.tname='諶燕')
44、查詢兩門以上課程大於70分的同學的學號及其平均成績
select sno,avg(score) from sc
where sno in
(select sno from sc where sc.score > 70 group by sno having count(sno)>1) --學號分組
group by sno;
45、檢索“c002”課程分數小於90,按分數降序排列的同學學號
select * from course;
select * from sc ;
select sc.sno "學號",sc.cno "課程號",sc.score "課程分數" from sc
where sc.cno = 'c002' and sc.score < 90 order by sc.score desc; --這里數據少,
46、刪除“s002”同學的“c001”課程的成績
delete from sc where sno='s002' and cno='c001';
1、查詢“c001”課程比“c002”課程成績高的所有學生的學號; --方法一: select m.* from (select * from sc a where a.cno='c001') m,--分組課程 (select * from sc b where b.cno='c002') n where m.sno = n.sno and m.score > n.score;--學號一樣,課程成績比較 --方法二: select * from sc a where a.cno='c001' and exists(select * from sc b where b.cno='c002' and a.score>b.score and a.sno = b.sno); --在學號一樣時,存在一個b的課程成績小於a 2、查詢平均成績大於60 分的同學的學號和平均成績; select c.sno "學號",avg(c.score) "平均成績" from sc c group by c.sno having avg(c.score) > 60; 3、查詢所有同學的學號、姓名、選課數、總成績; select * from sc; select *from student; select a.*,s.sname from (select sno,sum(score),count(cno) from sc group by sno) a ,student s where a.sno=s.sno; --在兩表中學號一樣的進行學號、總成績、選課數分組 4、查詢姓“劉”的老師的個數; select count(*) "姓 劉 的老師個數" from teacher t where t.tname like '劉%'; 5、查詢沒學過“諶燕”老師課的同學的學號、姓名; --放法一: select s.sno "學號",s.sname "姓名" from student s where s.sno not in (select distinct s.sno --去掉重復學號 from sc s, (select c.* from course c ,-- c總課程 (select tno from teacher t where tname='諶燕')t -- t是 諶燕 老師的課程 where c.tno=t.tno) b -- b是總課程與諶燕老師交集 where s.cno = b.cno ) --條件 課程一樣 select * from teacher ; select * from course ; --方法二: select st.sno "學號",st.sname "姓名" from student st where st.sno not in(select distinct s.sno --去學號重復的 from sc s join course c on s.cno=c.cno join teacher t on c.tno=t.tno --要求表sc和表course的課程一樣,表course和表teacher的課程編號一樣 and tname='諶燕') --也可以用 where 6、查詢學過“c001”並且也學過編號“c002”課程的同學的學號、姓名; select st.sno "學號",st.sname "姓名" from sc s join sc a on s.sno = a.sno --學號是一樣的 join student st on s.sno = a.sno where s.cno = 'c001' and a.cno = 'c002' and st.sno=s.sno; 7、查詢學過“諶燕”老師所教的所有課的同學的學號、姓名; select st.sno"學號",st.sname "姓名",s.cno "課程" from student st join sc s on st.sno=s.sno --學號一樣 join course c on s.cno=c.cno --再課程一樣 join teacher t on c.tno=t.tno --課程編號一樣 where t.tname='諶燕'; 8、查詢課程編號“c002”的成績比課程編號“c001”課程低的所有同學的學號、姓名; select /*st.sno "學號",st.sname "姓名"*/* from student st join sc s1 on st.sno = s1.sno join sc s2 on st.sno = s2.sno where s1.cno ='c001' and s2.cno = 'c002' and s1.score > s2.score; 9、查詢所有課程成績小於60 分的同學的學號、姓名; select /*st.sno "學號",st.sname "姓名"*/* from student st join sc s on st.sno = s.sno /*join course c on s.cno=c.cno*/--也可以加上 where s.score < 60; 10、查詢沒有學全所有課的同學的學號、姓名; select stu.sno,stu.sname,count(sc.cno) from student stu left join sc on stu.sno=sc.sno --student和sc左連接 group by stu.sno,stu.sname having count(sc.cno)<(select count(distinct cno)from course) 11、查詢至少有一門課與學號為“s001”的同學所學相同的同學的學號和姓名; select st.* from student st, (select distinct a.sno from --去掉重復學號 (select * from sc) a, (select * from sc where sc.sno='s001') b --sc表中學號為s001的作為一個表對象 where a.cno=b.cno) h --a、b表 課程一樣 where st.sno=h.sno and st.sno<>'s001';--學號不為s001的 12、查詢至少學過學號為“s001”同學所有一門課的其他同學學號和姓名; select * from sc left join student st --左連接 on st.sno=sc.sno --學號一樣 where sc.sno<>'s001' --學號不為s001的 and sc.cno in(select cno from sc where sno='s001'); --查詢學號為s001的所有課程信息 13、把“SC”表中“諶燕”老師教的課的成績都更改為此課程的平均成績; update sc c set score=(select avg(c.score) from course a,teacher b where a.tno=b.tno and b.tname='諶燕' and a.cno=c.cno group by c.cno) --課程分組 where cno in( --限定條件 select cno from course a,teacher b where a.tno=b.tno and b.tname='諶燕'); --課程中所教老師為 諶燕 14、查詢和“s002”號的同學學習的課程完全相同的其他同學學號和姓名; select sc.sno,st.sname from student st,sc where st.sno=sc.sno group by sc.sno,st.sname --以學號、姓名分組 having count(*)=(select count(*) from sc where sno='s002' group by sno) --與s002課程數量一樣的 and sc.sno!='s002'; --除學號為s002的其他同學 15、刪除學習“諶燕”老師課的SC 表記錄; deleted from sc where sc.cno --課程限定 in( select cno from course c left join teacher t on c.tno=t.tno --左連接,教師編號 where t.tname='諶燕'); select * from teacher; 16、向SC 表中插入一些記錄,這些記錄要求符合以下條件:沒有上過編號“c002”課程的同學學號、“c002”號課的平均成績; select * from sc; savepoint B; --回滾點B insert into sc (sno,cno,score) --學號,課程編號,成績 select distinct st.sno,sc.cno, (select avg(score) from sc where cno='c002') --去重,加上學號、課程編碼、c002的平均成績 from student st,sc where not exists --不存在 (select * from sc where cno='c002' and sc.sno=st.sno) and sc.cno='c002'; savepoint A; --回滾點A rollback B; --回滾到B 17、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分 select s.cno,max(s.score),min(s.score) from sc s group by s.cno;--在表sc中以課程分組 18、按各科平均成績從低到高和及格率的百分數從高到低順序 select cno "課程編號",avg(score) "課程平均成績",sum(case when score>=60 then 1 else 0 end)/count(*) "課程及格率" from sc group by cno order by avg(score) , /*sum(case when score>=60 then 1 else 0 end)/count(*)*/課程及格率 desc 19、查詢不同老師所教不同課程平均分從高到低顯示 select max(t.tno),max(t.tname),max(c.cno),max(c.cname),avg(score) --最大教師編號、教師名字、課程編號、課程名字、課程平均分 from sc,course c,teacher t --三個主要表 where sc.cno = c.cno and c.tno = t.tno --課程編號、教師編號一樣 group by c.cno --課程編號分組 order by avg(score) desc; --以課程平均分從高到低排序 20、統計列印各科成績,各分數段人數:課程ID,課程名稱,[100-85],[85-70],[70-60],[ <60] select sc.cno "課程ID",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 85 then 1 else 0 end) AS "[85-70]", sum(case when score >= 60 and score < 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.cno=c.cno group by sc.cno ,c.cname; 21、查詢各科成績前三名的記錄:(不考慮成績並列情況) /*select * from (select sno,cno,score,row_number()over(partition by cno order by score desc) rn from sc) where rn<4;*/ 22、查詢每門課程被選修的學生數 select s.cno "課程編號",count(s.cno) "選修人數" from sc s group by s.cno;--分組統計 select * from course; select * from sc; 23、查詢出只選修了一門課程的全部學生的學號和姓名 select s.sno "學號",st.sname "姓名" from sc s join student st on s.sno = st.sno --學號一樣 group by st.sname,s.sno --以姓名、學號分組 having count(cno)=1; --統計課程編碼為1的 24、查詢男生、女生人數 select * from student; select st.ssex "性別",count(*) "人數" from student st group by st.ssex; 25、查詢姓“張”的學生名單 select * from student st where st.sname like '張%'; 26、查詢同名同性學生名單,並統計同名人數 select st.sname,count(*) from student st group by st.sname; --姓名分組,統計姓名個數 select st.sname,count(*) from student st group by st.sname having count(*)>1; --姓名分組,統計同名個數 27、1995 年出生的學生名單(注:Student 表中Sage 列的類型是number) select sno,sname,sage,ssex from student t where to_char(sysdate,'yyyy')-sage = 1995;--將日期型轉為字符型,相減 28、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列 select sc.cno "課程ID", avg(sc.score) "課程平均成績" from sc group by sc.cno --按課程號分組 order by avg(sc.score) asc, sc.cno desc; 先按課程平均成績升序,再按課程號降序 29、查詢平均成績大於75 的所有學生的學號、姓名和平均成績 select sc.sno "學號",st.sname "姓名",avg(sc.score) "平均成績" from sc join student st on st.sno = sc.sno group by sc.sno,st.sname --分組要與上面的 學號、姓名 對應 having avg(sc.score) > 75; 30、查詢課程名稱為“SSH”,且分數低於60 的學生姓名和分數 select * from course; select * from sc; select * from student; select st.sname,sc.score from sc,student st,course c where st.sno=sc.sno and sc.cno=c.cno and c.cname='SSH' and sc.score < 60;--相同項作等 31、查詢所有學生的選課情況; select sc.sno "學號",st.sname "姓名",sc.cno "課程ID",c.cname "課程名稱",t.tname "授課教師" from student st, course c,sc,teacher t where st.sno=sc.sno and sc.cno=c.cno and c.tno = t.tno; 32、查詢任何一門課程成績在70 分以上的姓名、課程名稱和分數; select st.sname "姓名",c.cname "課程名稱",sc.score "分數" from sc,course c,student st where sc.sno = st.sno and c.cno = sc.cno and sc.score > 70; 33、查詢小於75的課程,並按課程號從大到小排列 select c.cno "課程號", c.cname "課程名稱",sc.score "課程分數" from sc,course c where sc.cno = c.cno and sc.score < 75 order by c.cno desc;--按課程號降序 34、查詢課程編號為c001 且課程成績在80 分以上的學生的學號和姓名; select st.sno "學號",st.sname "姓名",sc.score "分數" from sc,student st --只需要sc和student表 where sc.sno = st.sno and sc.cno = 'c001' and sc.score > 80; 35、求選了課程的學生人數 select count(distinct sc.sno) from sc; --只需統計sc表中學號不重復的個數 36、查詢選修“諶燕”老師所授課程的學生中,成績最高的學生姓名及其成績 select st.sname "學生姓名",sc.score "分數" from sc,student st,course c,teacher t where sc.sno = st.sno and c.tno = t.tno and sc.cno = c.cno and t.tname = '諶燕' and sc.score = (select max(sc.score) from sc where sc.cno = c.cno ); --這里后面必須要用sc.cno = c.cno區別每一個課程 37、查詢各個課程及相應的選修人數 select c.cno,count(sc.cno) from course c,sc where sc.cno = c.cno group by c.cno;--與22題一樣,這里用兩個表,需要課程名一樣 38、查詢不同課程成績相同的學生的學號、課程號、學生成績 select /*a.sno "學號",a.cno "課程號",a.score "學生成績" */ a.* from sc a ,sc b where a.score = b.score and a.cno<>b.cno; --成績一樣、課程不一樣 39、查詢每門功課成績最好的前兩名 /*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;*/ 40、統計每門課程的學生選修人數(超過10 人的課程才統計)。要求輸出課程號和選修人數, 查詢結果按人數降序排列,若人數相同,按課程號升序排列 select cno,count(sno) from sc group by cno having count(sno)>10 --超過10人的課程 order by count(sno) desc,cno asc; --先按人數降序,再按課程降序 41、檢索至少選修兩門課程的學生學號 select sc.sno from sc group by sno having count(cno) > 1;/* having count(sno)>1*/ 42、查詢全部學生都選修的課程的課程號和課程名 select distinct(c.cno),c.cname from sc,course c where sc.cno = c.cno; || select cno,cname from course c where c.cno in(select cno from sc group by cno) 43、查詢沒學過“諶燕”老師講授的任一門課程的學生姓名 select st.sname from student st where st.sno not in (select distinct sc.sno from sc,course c,teacher t --學號去重 where sc.cno=c.cno and c.tno=t.tno and t.tname='諶燕') 44、查詢兩門以上課程大於70分的同學的學號及其平均成績 select sno,avg(score) from sc where sno in (select sno from sc where sc.score > 70 group by sno having count(sno)>1) --學號分組 group by sno; 45、檢索“c002”課程分數小於90,按分數降序排列的同學學號 select * from course; select * from sc ; select sc.sno "學號",sc.cno "課程號",sc.score "課程分數" from sc where sc.cno = 'c002' and sc.score < 90 order by sc.score desc; --這里數據少, 46、刪除“s002”同學的“c001”課程的成績 delete from sc where sno='s002' and cno='c001';
存筆記