oracle:sql查询
生活随笔
收集整理的這篇文章主要介紹了
oracle:sql查询
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近遇到一些查詢,自己還不是能保證一口氣就寫對,經過和好友一起研究,有一兩個比較好的思路,尤其是最后兩個查詢,值得深思。為紀念這一丁丁進步和思維的一小步跨越,現記錄如下:
--1.查詢所有同學的學號、姓名、選課數、總成績 select sc.sno,sname,count(sc.cno),sum(sc.grade) from student,sc where student.sno=sc.sno group by sc.sno,sname order by sc.sno; --2.查詢姓“趙”的老師的個數 select count(sno) from student where sname like '趙%'; --3.查詢選課門數大于2的學生的學號和姓名 select sc.sno,sname,count(sc.sno) from student,sc where student.sno = sc.sno group by sc.sno,sname having count(sc.sno)>2 order by sc.sno; --4.查詢每個學生的選課最高分和最低分,列出學號和分數 select sno, max(grade),min(grade) from sc group by sno; --5.查詢選課門數最多的2個學生的學號和姓名(有問題) select sno,num from (select sc.sno,count(sc.cno) num from sc group by sc.sno order by num desc) t where rownum<=2 --6.一句SQL查出所有課程成績最高和最低的學生及其分數 select * from (select sc.cno,sc.sno,sc.grade from (select cno,max(grade) gr from sc group by cno) t,sc where sc.cno = t.cno and t.gr = sc.grade order by cno) union all select * from (select sc.cno,sc.sno,sc.grade from (select cno,min(grade) gr from sc group by cno) t,sc where sc.cno = t.cno and t.gr = sc.grade order by cno) order by 1 asc,3 desc實驗數據:student表、course表、sc表依次如下圖所示:
總結
以上是生活随笔為你收集整理的oracle:sql查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle:平均分大于78的女同学的学
- 下一篇: win10:JDK12.0.1环境变量配