實驗四SQL進行複雜查詢

來源:互聯網
上載者:User

標籤:des   資料   on   ad   ef   as   資料庫   sql   c   

select student.sno,sname,ssex,sage,sdept,cno,grade
from student,sc
where student.sno=sc.sno//?查詢每個學生及其選課情況;

select first.cno,second.cpno
from course first,course second
where first.cpno=second.cno //?查詢每門課的間接先修課

select student.sno,sname,ssex,sage,sdept,cno,grade
from student left outer join sc on student.sno=sc.sno//??將STUDENT,SC進行右串連

select student.sno,sname
from student inner join sc on student.sno=sc.sno
where cno=‘3‘ and sc.sno in
(select sno
from sc
where cno=‘2‘)//查詢既選修了2號課程又選修了3號課程的學生姓名、學號;

select student.sno,sname
from student
where sname!=‘劉晨‘ and sage=
(select sage
from student
where sname=‘劉晨‘)//?查詢和劉晨同一年齡的學生

select sname,sage
from student
where sno in
(select sno
from sc
where cno in
(select cno
from course
where cname=‘資料庫‘))//?選修了課程名為“資料庫”的學生姓名和年齡

select student.sno,sname
from student
where sdept<>‘IS‘ and
sage<all
(select sage
from student
where sdept=‘IS‘)//?查詢其他系比IS系任一學生年齡小的學生名單

select student.sno,sname
from student
where sdept<>‘IS‘ and
sage<any
(select sage
from student
where sdept=‘IS‘)//?查詢其他系中比IS系所有學生年齡都小的學生名單

select sname
from student
where Sno in
(select Sno from SC
group by Sno
having count(*) = (select count(*) from course ))//?查詢選修了全部課程的學生姓名

select student.sno,sname
from student
where sdept=‘IS‘ and ssex=‘男‘//?查詢電腦系學生及其性別是男的學生

select sno
from sc
where cno=‘1‘ except
select sno
from sc
where cno=‘2‘//?查詢選修課程1的學生集合和選修2號課程學生集合的差集

select cno
from course
where cno not in
(select cno
from sc
where sno in
(select sno
from student
where sname=‘李勇‘))//?查詢李勇同學不學的課程的課程號

select AVG(sage) as avgsage
from student inner join sc on student.sno=sc.sno
where cno=‘3‘//?查詢選修了3號課程的學生平均年齡

select cno,AVG(grade) as avggrade
from sc
group by cno//求每門課程學生的平均成績

select course.cno ‘課程號‘, count(sc.sno) ‘人數‘
from course,sc
where course.cno=sc.cno
group by course.cno having count(sc.sno)>3 order by COUNT(sc.sno) desc,course.cno asc//?統計每門課程的學生選修人數(超過3人的才統計)。要求輸出課程號和選修人數,結果按人數降序排列,若人數相同,按課程號升序排列

select sname
from student
where sno>
(select sno from student where sname=‘劉晨‘)and
sage<(select sage from student where sname=‘劉晨‘)//?查詢學號比劉晨大,而年齡比他小的學生姓名。

select sname,sage
from student
where ssex=‘男‘and sage>
(select MAX(sage) from student where ssex=‘女‘)//?求年齡大於所有女同學年齡的男同學姓名和年齡

實驗四SQL進行複雜查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.