日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

经典50例

發布時間:2023/12/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 经典50例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

student(sno,sname,sage,ssex) 學生表
course(cno,Cname,tno) 課程表
sc(sno,cno,score) 成績表
teacher(tno,Tname) 教師表

/*例1:查詢“c1”課程比“c2”課程高的所有學生*/ select a.sno from (select sno,score from sc where cno='c1') a,(select sno,score from sc where cno='c2') b where a.sno=b.sno and a.score>b.score; /*例2:查詢平均成績大于的同學的學號和平均成績*/ select sno,avg(score) from sc group by sno having avg(score)>60; /*例3:查詢所有同學的學號、姓名、選課數、總成績*/ select student.sno,sname,count(cno),sum(score) from student,sc where student.sno=sc.sno group by student.sno,sname; /*例4:查詢姓“李”的老師的個數*/ select count(distinct(tname)) from teacher where tname like '李%'; /*例5:查詢沒學過“李玉”老師課的同學的學號和姓名*/ select sno,sname from student where sno not in ( select distinct(sc.sno) from course,sc,teacher where sc.cno=course.cno and course.tno=teacher.tno and tname='李玉' ); /*例:6:查詢學過“c1”并且也學過“c2”課程的的同學的學號和姓名*/ select student.sno,sname from student,sc a,sc b where student.sno=a.sno and student.sno=b.sno and a.cno='c1' and b.cno='c2'; /*另一種做法*/ select student.sno,sname from student,sc where student.sno=sc.sno and cno='c1' and exists (select * from sc a where sc.sno=a.sno and a.cno='c2'); /*例7:查詢學過“李玉”老師所教的所有課的同學的學號、姓名*/ select sno,sname from student where sno in ( select sno from course,sc,teacher where sc.cno=course.cno and course.tno=teacher.tno and teacher.tname='李玉' group by sno having count(course.cno)=(select count(cno) from course,teacher where course.tno=teacher.tno and tname='李玉' )); /*例8:查詢課程編號“c1”的成績比課程編號“c2”課程低的所有同學的學號、姓名*/ select sno,sname from (select student.sno,sname,score,(select score from sc a where a.sno=student.sno and a.cno='c2') score2 from student,sc where student.sno=sc.sno and cno='c1') b where score2>score; /*另一種做法*/ select student.sno,sname from student,sc a,sc b where student.sno=a.sno and student.sno=b.sno and a.cno='c1' and b.cno='c2' and a.score<b.score; /*例9:查詢所有課程成績小于60分的同學的學號、姓名*/ select sno,sname from student where sno not in (select a.sno from student a,sc where a.sno=sc.sno and score>=60); /*存在一個問題:沒有選課的同學也會被篩選出來*/ /*例10:查詢沒有學全所有課的同學的姓名、學號*/ select student.sno,sname from student,sc where student.sno=sc.sno group by student.sno,sname having count(cno)< (select count(cno) from course) --存在問題:沒有篩選出沒有選課的同學# /*例11:查詢至少有一門課與學號為“1”的同學所學相同的同學的學號和姓名*/ select student.sno,sname from student,sc where student.sno=sc.sno and cno in (select cno from sc where sno='1') and student.sno!='1'; /*例12:查詢至少學過學號為“1”同學所有一門課的其他同學的學號和姓名*/ select distinct(student.sno),sname from student,sc where student.sno=sc.sno and cno in (select cno from sc where sno='1') and student.sno!='1'; /*例13:把“sc”表中“李玉”老師教的課的成績都改為此課程的平均成績*/ --與在mysql中的區別 update SC set score=(select avg(SC_2.score)from SC SC_2 where SC_2.Cno=SC.cno ) from Course,Teacher where Course.cno=SC.cno and Course.tno=Teacher.tno and Teacher.Tname='李玉'; /*例14:查詢和“2”號的同學學習的課程完全相同的其他同學學號和姓名*/ select sc.sno,sname from sc inner join student on student.sno=sc.sno where cno in (select cno from sc where sno='2') group by sc.sno,sname having count(*)=(select count(*) from sc where sno='2') /*例15:刪除學習“葉平”老師課的sc表的記錄*/ delete from sc where cno=(select cno from course,teacher where course.tno=teacher.tno and tname='李玉'); /*例16:向SC表中插入一些記錄,符合以下:沒有上過編號“”課程的同學學號,2號課的平均成績*/ insert into sc(sno,cno,score) select sno,'c2',(select avg(score) from sc where cno='c2') from student where sno not in (select sno from sc where cno='c2'); /*例17:按平均成績從高到低顯示所有學生的“管理學”、“經濟學”、“R語言”三門的課程成績,按如下形式顯示:學生ID,管理學,經濟學,R語言,有效課程數,平均分*/ select sno as 學生ID, (select score from course,sc where t.sno=sc.sno and sc.cno=course.cno and cname='管理學') as 管理學, (select score from course,sc where t.sno=sc.sno and sc.cno=course.cno and cname='經濟學') as 經濟學, (select score from course,sc where t.sno=sc.sno and sc.cno=course.cno and cname='R語言') as R語言, count(*) as 有效課程數,avg(t.score) as 平均成績 from sc t group by sno order by avg(t.score); /*例18:查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分*/ select cno as 課程ID,max(score) as 最高分,min(score) as 最低分 from sc group by cno; /*例19:按各科平均成績從低到高和及格率的百分數從高到低順序*/ select sc.cno,cname,avg(score) as 平均數,(100 * SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*)) as 及格百分數 from sc,course where sc.cno=course.cno group by sc.cno,cname order by 平均數 ASC,及格百分數 DESC; /*例20:查詢如下課程平均成績和及格率的百分數(用"1行"顯示):管理學(c1),經濟學(c2),R語言(c3))*/ select sum(case when cno='c1' then score else 0 end)/sum(case cno when 'c1' then 1 else 0 end) as 管理學平均分, 100 * SUM(CASE WHEN cno='c1' and isnull(score,0)>=60 THEN 1 ELSE 0 END)/sum(case when cno='c1' then 1 else 0 end) as 管理學及格百分數, sum(case when cno='c2' then score else 0 end)/sum(case cno when 'c2' then 1 else 0 end) as 經濟學平均分, 100 * SUM(CASE WHEN cno='c2' and isnull(score,0)>=60 THEN 1 ELSE 0 END)/sum(case when cno='c1' then 1 else 0 end) as 經濟學學及格百分數, sum(case when cno='c3' then score else 0 end)/sum(case cno when 'c3' then 1 else 0 end) as R語言平均分, 100 * SUM(CASE WHEN cno='c3' and isnull(score,0)>=60 THEN 1 ELSE 0 END)/sum(case when cno='c1' then 1 else 0 end) as R語言及格百分數 from sc; /*例21:查詢不同老師所教不同課程平均分從高到低顯示*/ select tno,sc.cno,avg(score) from sc,course where sc.cno=course.cno group by tno,sc.cno order by avg(score) DESC; /*例22:查詢如下課程成績第名到第6 名的學生成績單:管理學(c1),經濟學(c2),R語言(c3) [學生ID],[學生姓名],管理學,經濟學,R語言,平均成績*/--例23:統計列印各科成績,各分數段人數:課程ID,課程名稱,[100-85],[85-70],[70-60],[ <60] select sc.cno as 課程ID,cname as 課程名稱, 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 between 60 and 0100 then 1 else 0 end)as [70-60], sum(case when score<60 then 1 else 0 end)as [<60] from sc,course where sc.cno=course.cno group by sc.cno,cname;--例24:查詢學生平均成績及其名次 SELECT 1+(SELECT COUNT( distinct 平均成績) FROM (SELECT sno,AVG(score) AS 平均成績 FROM SC GROUP BY sno) AS T1 WHERE 平均成績 > T2.平均成績) as 名次, sno as 學生學號,平均成績 FROM (SELECT sno,AVG(score) 平均成績 FROM SC GROUP BY sno) AS T2 ORDER BY 平均成績 desc;--例25:查詢各科成績前三名的記錄:(不考慮成績并列情況) select cno,sno,score from sc where score in(select top 3 score from sc x where x.cno=sc.cno order by score desc) order by cno;--例26:查詢每門課程被選修的學生數 select cno,COUNT(*)as 人數 from sc group by cno; --例27:查詢出只選修了一門課程的全部學生的學號和姓名 select sno,sname from student where sno in ( select sno from sc where sc.sno=student.sno group by sno having COUNT(*)=1);--例28:查詢男生、女生人數 select 男生人數,女生人數 from (select COUNT(*)as 女生人數 from student where ssex='女')as a, (select COUNT(*)as 男生人數 from student where ssex='男') as b;Select count(Ssex) as 男生人數 from Student group by Ssex having Ssex='男'; Select count(Ssex) as 女生人數 from Student group by Ssex having Ssex='女'; --例29:查詢姓“張”的學生名單 select sname from student where sname like '張%'; --例30:查詢同名同性學生名單,并統計同名人數 select sname,COUNT(*) from student group by sname having COUNT(*)>1 --例31:年出生的學生名單(注:Student表中Sage列的類型是datetime) --修改表的類型 alter table student alter column sage datetime;select Sname, CONVERT(char (11),DATEPART(year,Sage)) as age from student where CONVERT(char(11),DATEPART(YYYY,Sage))='1981'; --例32:查詢每門課程的平均成績,結果按平均成績升序排列,若相同,則按課程號降序排列 select cno,AVG(score)as 平均成績 from sc group by cno order by AVG(score) asc,cno desc; --例33:查詢平均成績大于的所有學生的學號、姓名和平均成績 select sc.sno,sname,AVG(score) from student,sc where student.sno=sc.sno group by sc.sno,sname having AVG(score)>80; --例34:查詢課程名稱為“管理學”,且分數低于的學生姓名和分數 select sname,isnull(score,0) from student,sc,course where student.sno=sc.sno and sc.cno=course.cno and cname='管理學' and score<60; --例35:查詢所有學生的選課情況 select sno,COUNT(*) as 選課數 from sc group by sno;SELECT SC.Sno,Sname,SC.cno,Cname FROM SC,Student,Course where SC.sno=Student.sno and SC.cno=Course.cno; --例36:查詢任何一門課程成績在分以上的姓名、課程名稱和分數 select distinct(sname),cname,score from student,course,sc where student.sno=sc.sno and sc.cno=course.cno and score>70; --例37:查詢不及格的課程,并按課程號從大到小排列 select distinct(cno) from sc where score<60 order by cno desc; --例38:查詢課程編號為c3且課程成績在分以上的學生的學號和姓名 select student.sno,sname from student,sc where student.sno=sc.sno and cno='c3' and score>80; --例39:求選了課程的學生人數 select count(distinct(sno)) from sc; --例40:查詢選修“葉平”老師所授課程的學生中,成績最高的學生姓名及其成績 select sname,MAX(score) from sc,student where sc.sno=student.sno and cno in (select cno from teacher,course where teacher.tno=course.tno and tname='李玉') group by sname; --另一種做法 select Student.Sname,score from Student,SC,Course C,Teacher where sc.sno=student.sno and SC.cno=C.cno and C.tno=Teacher.tno and Teacher.Tname='李玉' and SC.score= (select max(score)from SC where Cno=C.cno); --例41:查詢各個課程及相應的選修人數 select cno,COUNT(*) from sc group by cno; --例42:查詢不同課程成績相同的學生的學號、課程號、學生成績 select distinct A.sno,B.score from SC A ,SC B where A.Score=B.Score and A.cno <>B.cno; /*例43:查詢每門功成績最好的前兩名*/ select sno as 學生ID,cno as 課程ID,score as 分數 from sc where score in (select top 2 score from sc y where y.cno=sc.cno order by score desc) ; /*例44:統計每門課程的學生選修人數。查詢結果按人數降序排列,若人數相同,按課程號升序排列*/ select cno as 課程號,count(*) as 人數 from sc group by cno order by count(*) desc,cno asc; /*例45:檢索至少選修兩門課程的學生學號*/ select sno from sc group by sno having count(*)>=2; /*例46:查詢全部學生都選修的課程的課程號和課程名*/ /*同理:查詢選修了全部課程的學生姓名*/ select cno,cname from course where not exists (select * from student where not exists (select * from sc where sc.sno=student.sno and sc.cno=course.cno)); --下面一種方法更簡單,全部課程都在學生選的課程中 select cno,cname from course where cno in (select cno from sc group by cno); /*例47:查詢沒學過“李玉”老師講授的任一門課程的學生姓名*/ select sname from student where sno not in( select sno from sc,course,teacher where sc.cno=course.cno and course.tno=teacher.tno and tname='李玉'); /*例48:查詢兩門以上不及格課程的同學的學號及平均成績*/ select sno,avg(isnull(score,0)) from sc where sno in(select sno from sc where score<60 group by sno having count(*)>=2) group by sno; /*例49:檢索“c2”課程分數小于*,按分數降序排列的同學學號*/ select sno from sc where cno='c2' and score<60 order by score DESC; /*例50:刪除“”同學的“c1”課程的成績*/ delete from sc where sno='2'and cno='c1';

總結

以上是生活随笔為你收集整理的经典50例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。