经典50例
2015-10-19 15:44
344 查看
student(sno,sname,sage,ssex) 学生表
course(cno,Cname,tno) 课程表
sc(sno,cno,score) 成绩表
teacher(tno,Tname) 教师表
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';
相关文章推荐
- leetcode_13_Roman to Integer (easy)
- C#操作Mongodb的心得
- 如何利用cookie实现html页面的记住密码
- 电信免费手机卡iFree黄金版使用须知(四)
- tomcat部署项目
- Android Touch事件分发
- struts2配置文件解析
- 守护线程
- iOS程序启动原理和UIApplication
- 去除空白cell的分割线
- 富文本Attributes详解
- 算法的前提条件
- C与OC、C++的区别
- Python scrapy run with Unhandled error in Deferred解决方案
- Android 设置页面 及 背景音乐 的实现
- exits和in的区别
- Java7中的switch...case支持string的实现细节
- tmux 基础教程
- 添加自动远程连接的脚本,自动输入密码,节省时间。
- OAF中下载附件之后页面失效,报浏览器后退异常