多表的使用問題

--9: 查詢全部同窗的學號、姓名、選課數、總成績;
--select st1.Sid,st1.Sname,sc2.score from Student st1,SC sc2
-- where st1.Sid = sc2.sid
 
select t.Sid 學號,t.Sname 姓名,COUNT(Sid) 選課數,SUM(score) 成績
 from (select st1.Sid,st1.Sname,sc2.score from Student st1,SC sc2
   where st1.Sid = sc2.sid) t
  group by t.Sname ,t.Sid 數據庫

--10: 查詢所有學生都選修的課程的課程號和課程名
/*
select sc.cid,COUNT(sc.score)
 from SC sc,Student st
  group by sc.cid having COUNT(sc.sid) = COUNT(st.Sid)ci

select cid ,COUNT(cid) from SC sc group by sc.cidselect

select sc.cid,COUNT(sc.sid) from SC sc group by sc.cid
 having COUNT(sc.sid)=(select count(st.Sid) from Student st )數據

*/
select co.Cid,co.Cname
 from (select sc.cid from SC sc group by sc.cid
  having COUNT(sc.sid)=(select count(st.Sid) from Student st)) t
  join Course co
   on co.Cid = t.cid
   
--11: 查詢課程名稱爲「數據庫」,且分數低於60的學生姓名和分數
/*
select Cid from Course where Cname like '數據庫'查詢

select sc.sid ,sc.score,sc.cid
 from SC sc,Course co
  where (co.Cname='數據庫' and sc.score<60)
  
select co.Cid
 from Course co
  where co.Cname like '數據庫'
  
  select sc.sid , sc.score ,sc.cid
 from SC sc
  where sc.score<60 and sc.cid = (select co.Cid
   from Course co where co.Cname like '數據庫')
  
*/  vi

     
select st.Sname ,sc.score
 from (select sc.sid , sc.score ,sc.cid from SC sc
   where sc.score<60 and sc.cid = (select co.Cid
    from Course co where co.Cname like '數據庫')) sc
  join Student st
   on st.Sid = sc.sid
  co

相關文章
相關標籤/搜索