sql查询语句(教学)

thslovehh 2010-03-03 08:01:56
关于教学数据库的三个基本表
学生 S(S#,SNAME,AGE,SEX); 学习 SC(S#,C#,GRADE); 课程 C(C#,CNAME,TEACHER)
(1)统计有学生选修的课程门数.


(2)求选修C4课程的学生的平均年龄.


(3)求LIU老师所授课程的每门课程的学生平均成绩.


(4)统计每门课程的学生选修人数(超过10人的课程才统计).要求输出课程号和选修人数, 查 询结果按人数降序排列,若人数相同,按课程号升序排列.


(5)检索学号比WANG同学大,而年龄比他小的学生姓名.


(6)检索姓名以WANG打头的所有学生的姓名和年龄.


(7)在SC中检索成绩为空值的学生学号和课程号.


(8)求年龄大于女同学平均年龄的男学生姓名和年龄.
...全文
273 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
thslovehh 2010-03-03
  • 打赏
  • 举报
回复
哥,我第一次发帖、、、
fspop 2010-03-03
  • 打赏
  • 举报
回复
结帖率:0.00%....
thslovehh 2010-03-03
  • 打赏
  • 举报
回复
我也想解啊,问题是还没解出来啊,不敢随便解啊,各位都是高手、、、
黄_瓜 2010-03-03
  • 打赏
  • 举报
回复
引用楼主 thslovehh 的回复:
关于教学数据库的三个基本表
学生 S(S#,SNAME,AGE,SEX); 学习 SC(S#,C#,GRADE); 课程 C(C#,CNAME,TEACHER)
(1)统计有学生选修的课程门数.


(2)求选修C4课程的学生的平均年龄.


(3)求LIU老师所授课程的每门课程的学生平均成绩.


(4)统计每门课程的学生选修人数(超过10人的课程才统计).要求输出课程号和选修人数, 查 询结果按人数降序排列,若人数相同,按课程号升序排列.


(5)检索学号比WANG同学大,而年龄比他小的学生姓名.


(6)检索姓名以WANG打头的所有学生的姓名和年龄.


(7)在SC中检索成绩为空值的学生学号和课程号.


(8)求年龄大于女同学平均年龄的男学生姓名和年龄.


兄台你杂光发帖不解贴呢
--小F-- 2010-03-03
  • 打赏
  • 举报
回复
SQL语句练习题参考答案 
1、 select Sname,Ssex,Class from Student;
2、 select distinct depart from teacher;
3、 select Sno as '学号',Sname as '姓名',Ssex as '性别',Sbirthday as'出生日期',Class as'班号'from student;

select Sno as 学号,Sname as 姓名,Ssex as 性别,Sbirthday as 出生日期,Class as 班号 from student;
4、 select * from score where degree between 60 and 80;
或select * from score where degree>=60 and degree<=80;
5、 select * from score where degree in (85,86,88);
6、 select * from student where class='95031'or Ssex='女';
7、 select * from student order by class desc;
8、 select * from score order by cno asc ,degree desc;
或select * from score order by cno ,degree desc;
9、 select count(*) as CNT from student where class='95031';
10、select Sno as '学号',cno as '课程号', degree as '最高分' from score
where degree=(select max(degree) from score)
11、select avg(degree)as 课程平均分 from score where cno='3-105';
12、select cno,avg(degree) from score where cno like'3%'group by cno having count(*) >5;
13、select Sno from score group by Sno having min(degree)>70 and max(degree)<90;
14、select student.Sname,score.Cno,score.degree from student,score where student.Sno=score.Sno;
15、select x.Sno,y.Cname,x.degree from score x,course y where x.Cno=y.Cno;
16、select x.Sname,y.Cname,z.degree from student x,course y,score z where x.Sno=z.Sno and z.Cno=y.Cno;
17、select y.Cno,avg(y.degree) from student x,score y where x.Sno=y.Sno and x.class='95033'group by y.cno;
18、select Sno,Cno,rank from score,grade where degree between low and upp order by rank;
19、select x.Cno,x.Sno,x.degree from score x,score y
where x.cno='3-105' and x.degree>y.degree and y.sno='109'and y.cno='3-105';
20、
1,查询成绩非本科最高 select * from score b where degree <(select max(degree) from score a where a.cno=b.cno);
2,查询成绩非本科最高并且选2门以上的学生的成绩:
21、select x.cno,x.Sno,x.degree from score x,score y where x.degree>y.degree and y.sno='109'and y.cno='3-105';
select cno,sno,degree from score where degree >(select degree from score where sno='109' and cno='3-105')
22、select sno,sname,sbirthday from student where to_char(sbirthday,'yyyy')=(select to_char(sbirthday,'yyyy') from student where sno='108');
23、select cno,sno,degree from score where cno=(select x.cno from course x,teacher y where x.tno=y.tno and y.tname='张旭');
24、select tname from teacher where tno in(select x.tno from course x,score y where x.cno=y.cno group by x.tno having count(x.tno)>5);
25、select * from student where class in('95033','95031');
26、select distinct cno from score where degree in (select degree from score where degree>85);
27、select * from score where cno in(select x.cno from course x,teacher y where y.tno=x.tno and y.depart='计算机系');
28、select tname,prof from teacher where depart='计算机系' and prof not in (select prof from teacher where depart='电子工程系');
29、select * from score where cno='3-105' and degree>any (select degree from score where cno='3-245')order by degree desc;
30、select * from score where cno='3-105' and degree>all(select degree from score where cno='3-245');
31、select tname,tsex,tbirthday from teacher
union select sname,ssex,sbirthday from student;
32、select tname,tsex,tbirthday from teacher where tsex='女'
union select sname,ssex,sbirthday from student where ssex='女';
33、select * from score a where degree<(select avg(degree)
from score b where a.cno=b.cno);
34、select tname,depart from teacher a where exists
(select * from course b where a.tno=b.tno);
35、select tname,depart from teacher a where not exists
(select * from course b where a.tno=b.tno);
36、select class from student where ssex='男'group by class having count(*)>=2;
37、select * from student where sname not like'王_';
38、select sname as 姓名,(to_char(sysdate,'yyyy')-to_char(sbirthday,'yyyy')) as 年龄 from student
39、select sname,sbirthday as 最大 from student where sbirthday =(select min (sbirthday) from student)
union select sname,sbirthday as 最小 from student where sbirthday =(select max(sbirthday) from student)
40、select class,sname,sbirthday from student order by class desc,sbirthday;
41、select x.tname,y.cname from teacher x,course y where x.tno=y.tno and x.tsex='男';
42、select * from score where degree=(select max(degree)from score);
43、select sname from student where ssex=(select ssex from student where sname='李军');
44、select sname from student where ssex=(select ssex from student where sname='李军') and class=(select class from student where sname='李军');
45、select * from score where sno in(select sno from student where ssex='男') and cno=(select cno from course
where cname='计算机导论');
jwdream2008 2010-03-03
  • 打赏
  • 举报
回复
2: select Avg(age) from  S where S# in (select S# from SC where C#=C4)
jwdream2008 2010-03-03
  • 打赏
  • 举报
回复
1: select count (distinct C#)  From SC
SQL Server 2005微软官方权威参考手册     是Inside Microsoft SQL Server 2005系列书中的第一本,SQL Server类的顶尖之作   全球公认SQL Server 2005经典著作,囊括大量鲜为人知的技术内幕,大师智慧、专家经验尽览无余。       本系列图书中文版得到了微软总部SQL Server组专家的高度重视,同时也得到了微软中国上海SQL Server全球技术支持中心的高度关注。        本书详细介绍了T-SQL的内部构造,包含了非常全面的编程参考。数据库开发人员和DBA可以通过书中的最佳实践、高级技巧和代码示例来掌握这门复杂的编程语言,以切合实际的方案来解决复杂的问题。本书涵盖了T-SQL程序设计的方方面面,如基于集合的编程技术、日期和时间相关的XML和CLR数据类型的使用、临时对象、T-SQL和CLR用户自定义函数、存储过程、触发器、事务和新的错误处理结构、应用并发模型支持并发用户、使用Service Broker来控制数据库应用程序中的异步处理等。   内容简介 本书是Inside Microsoft SQL Server 2005系列四本著作中的一本。它详细介绍了T-SQL的内部构造,包含了非常全面的编程参考。它提供了使用Transact-SQL(T-SQL)的专家级指导,T-SQL是用于SQL Server的最常见的也是功能最强大的编程语言。该书由Itzik Ben-Gan权威执笔,重点关注语言特性以及它们如何被SQL Server引擎解释和处理。   通过本书,你将深入了解T-SQL的高级用法,包括触发器、用户自定义函数、异常处理等。该书解释并比较了SQL Server 2000和SQL Server 2005在数据库开发相关问题上的解决方案,深入讨论了SQL Server 2005中新增的T-SQL编程特性,包含了大量的代码示例、表示例和逻辑难题以帮助数据库开发人员和管理员理解复杂的逻辑并掌握T-SQL。   本书适合于专业数据库开发者、BI开发者、DBA和以SQL Server作为后台数据库的一般应用程序开发者,读者可以通过书中的最佳实践、高级技巧和代码示例来掌握这门复杂的编程语言,以切合实际的方案来解决复杂的实际问题。 作者简介 Itzik Ben-Gan是Solid Quality Learning的导师和创始人。从1999年开始,他一直是Microsoft SQL Server MVP(最有价值专家),并在全世界已经开展过无数次T-SQL查询、T-SQL优化和编程方面的培训。Itzik是Microsoft SQL Server方面几本著作的作者。他在SQL ServerMagazine和MSDN上发表了许多文章和白皮书。Itzik被邀请在许多会议上做报告,包括TechEd、DevWeek、世界各地的各种SQL用户组、PASS、SQL server Magazine Connections和Solid Quality Learning的会议。 从1992年开始,Itzik就一直致力于涉及各种数据库和计算机系统相关技术的多个课题。除了帮助顾客处理紧迫的要求、修复问题、优化数据库、教学和担任顾问以外,他还帮助开发人员和数据库管理员转变关系/基于集合的理念,改善他们编写代码的性能和可维护性。Itzik擅长T-SQL查询、查询优化、编程和内部原理,此外他还精通其他的数据库领域。1999年,Itzik创立以色列SQL Server和OLAP用户组,一直管理至今。 目录 序 前言 致谢 引言 第1章 数据类型相关的问题,XML和CLR UDT 1.1 DATETIME数据类型 DATETIME的存储格式 时间处理   Datetime相关的查询问题  1.2 与字符相关的问题   模式匹配   区分大小写(Case-Sensitive)的筛选器  1.3 大型对象(Large Object,LOB)   MAX 说明符   BULK行集提供程序  1.4 隐式转换(Implicit Conversion)   标量表达式   筛选表达式  1.5 基于CLR的用户定义类型   UDT理论简介   开发UDT  1.6 XML数据类型   关系数据库中的XML支持   什么时候应该使用XML代替关系表现形式?   数据库中的XML序列化对象   使用开放架构(Open Schema)的XML   作为存储过程参数的XML数据类型   Xquery修改语句  1.7 结论 第2章 临时表和表变量  2.1 临时表   局部临时表   全局临时表  2.2 表变量   限制条件   tempdb   范围和可见性   事务上下文   统计信息  2.3 tempdb相关的注意事项  2.4 表表达式  2.5 比较临时对象  2.6 综合练习——关系分区(Relational Division)  2.7 结论 第3章 游标  3.1 使用游标  3.2 游标开销  3.3 单独处理每一行  3.4 按顺序访问   自定义聚合   连续聚合   最大并发会话   匹配问题  3.5 结论 第4章 动态SQL 第5章 视图 第6章 用户定义函数 第7章 存储过程 第8章 触发器 第9章 事务 第10章 错误处理 第11章 Service Broker 附录A CLR程序指南 A.1 创建CLRUtilities数据库: SQL Server A.2 部署:Visual Studio A.3 部署和测试:Visual Studio 和 SQL Server 索引 中英文术语对照表

34,873

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧