實驗八
聲明:所有SQL語句均在實驗平臺驗證通過,實驗細節可能隨時間推移老師會進行修改。在此僅提供解答思路,畢竟我的方法肯定不是最優,而且實驗平臺有查重功能,不要一昧的複製哦!
1.查詢各院系(不包括院系名稱爲空的)的數據結構平均成績avg_ds_score、操作系統平均成績avg_os_score,平均成績四捨五入到個位,創建表test8_01。
create table test8_01 as select ds.dname,ds.avg_ds_score,os.avg_os_score from
(select dname,round(avg(score),0) avg_ds_score from
(select * from
(select sid,max(score) score from pub.student_course where cid in
(select cid from pub.course where name='數據結構') group by sid) sc join
(select dname,sid from pub.student) s on sc.sid=s.sid where dname is not null)
group by dname) ds
join
(select dname,round(avg(score),0) avg_os_score from
(select * from
(select sid,max(score) score from pub.student_course where cid in
(select cid from pub.course where name='操作系統') group by sid) sc join
(select dname,sid from pub.student) s on sc.sid=s.sid where dname is not null)
group by dname) os on ds.dname=os.dname;
2.查詢”計算機科學與技術學院”的同時選修了數據結構、操作系統兩門課的學生的學號sid、姓名name、院系名稱dname、數據結構成績ds_score、操作系統成績os_score,創建表test8_02。
create table test8_02 as
select s.sid,s.name,s.dname,ds_score,os_score from
((select sid,max(score) ds_score from pub.student_course where cid in
(select cid from pub.course where name='數據結構') and
sid in (select sid from pub.student where dname='計算機科學與技術學院') group by sid) ds
join
(select sid,max(score) os_score from pub.student_course where cid in
(select cid from pub.course where name='操作系統') and
sid in (select sid from pub.student where dname='計算機科學與技術學院') group by sid) os
on ds.sid=os.sid
left join pub.student s on os.sid=s.sid);
3.查詢計算機科學與技術學院的選修了數據結構或者操作系統的學生的學號sid、姓名name、院系名稱dname、數據結構成績ds_score、操作系統成績os_score,創建表test8_03。
create table test8_03 as
select distinct a.sid,a.name,a.dname,ds_score,os_score from
(select distinct s.sid,s.name,s.dname,ds_score from
(select * from pub.student where dname='計算機科學與技術學院' and sid in
(select sid from pub.student_course where cid in (select cid from pub.course where name='數據結構' or name='操作系統'))
) s
left join
(select sid,max(score) ds_score from pub.student_course where cid in
(select cid from pub.course where name='數據結構') and
sid in (select sid from pub.student where dname='計算機科學與技術學院') group by sid) ds on s.sid=ds.sid) a
left join
(select sid,max(score) os_score from pub.student_course where cid in
(select cid from pub.course where name='操作系統') and
sid in (select sid from pub.student where dname='計算機科學與技術學院') group by sid) os on a.sid=os.sid;
4.查詢計算機科學與技術學院所有學生的學號sid、姓名name、院系名稱dname、數據結構成績ds_score、操作系統成績os_score,創建表test8_04。
create table test8_04 as
select distinct a.sid,a.name,a.dname,ds_score,os_score from
(select distinct s.sid,s.name,s.dname,ds_score from
(select * from pub.student where dname='計算機科學與技術學院' ) s
left join
(select sid,max(score) ds_score from pub.student_course where cid in
(select cid from pub.course where name='數據結構') and
sid in (select sid from pub.student where dname='計算機科學與技術學院') group by sid) ds on s.sid=ds.sid) a
left join
(select sid,max(score) os_score from pub.student_course where cid in
(select cid from pub.course where name='操作系統') and
sid in (select sid from pub.student where dname='計算機科學與技術學院') group by sid) os on a.sid=os.sid;
筆記:
1.第一小題先篩選出每個學生這兩門課的成績,然後按學院分組求平均。
2.第二小題分別查詢計算機科學與技術學院選修了數據結構和操作系統的同學,然後兩個做連接,最後只保留同時選修了兩門課的同學。
3.第三,第四小題與第二小題類似,但是根據題目的細微變化,要採用不同的連接方法。
4.而且對於2個表以上的連接,我建議把連接後的表定義爲一個新的表,再和第三個表做連接,否則可能結果會出現很多null的空連接,這是我測試多遍得出的。
ps:本人開通了個人的微信公衆號,希望大家能關注一下,
我會將資源、文章優先推送到公衆號上。
推送自己的一些學習筆記,實驗源代碼等,
歡迎大家互相交流學習。