您的位置:首页 > 数据库

教学思路SQL之入门习题《学员成绩》 三、多表复杂子查询

2009-05-16 22:33 429 查看
1、查询出所有大连地区的学生的成绩。

2、查询从来没有参加任何考试的学生的信息。

3、查询有考试成绩纪录的学生的信息.

4、将所有大连地区学生的课程编号为3的课程成绩加10分。

5、删除所有C#课的成绩。

6、查询课程编号为3的课程的平均成绩,显示有此门课程成绩的学生编号,成绩及距平均分之间的差值。

7、查询参加了所有考试的学生的姓名。

8、查询有成绩纪录的学生人数。

9、查询有2门以上成绩的学生的姓名和联系方式

10、查询哪些学生有c#考试成绩,显示这些学生的姓名

11.查询出科目中教师的姓名,同时增加编号。

12.随机显示学生信息表中的两条数据:使用newid()函数

13.查询出联系电话为null的学生信息,联系电话用‘无’代替。

14.将查询出的地区为大连的都显示为null.

15.查询科目平均成绩超过70分的教师姓名。

新知识:

对空值进行显示操作:

isnull(A,‘aa’)函数可以将空值转换为有效的值。将列名A的值null改为‘aa’显示

nullif(A,‘aa’)函数可以根据指定的条件来生成空值。将列名A的值aa改为null显示

newid函数:

使用newid()函数会产生随机行

答案

1、查询出所有大连地区的学生的成绩。

 select s.number,c.score from chengji as c ,student as s where c.number=s.number and s.diqu='大连'

 select * from chengji where number in( select number from student where diqu='大连')

2、查询从来没有参加任何考试的学生的信息。

select * from student where number not in (select number from chengji)

3、查询有考试成绩纪录的学生的信息.

select * from student where number in (select number from chengji)

4、将所有大连地区学生的课程编号为3的课程成绩加10分。

update chengji set score=score+10 where scorenumber=3

5、删除所有C#课的成绩。

delete chengji where scorenumber=(select scorenumber from score where kemu='C#')

6、查询课程编号为3的课程的平均成绩,显示有此门课程成绩的学生编号,成绩及距平均分之间的差值。

select number,score-(select avg(score) from chengji where scorenumber=3) from chengji where scorenumber=3

7、查询参加了所有考试的学生的姓名。

select name from student where number in( select max(number) from chengji group by number)

8、查询有成绩纪录的学生人数。

selectnumber ,name from student where number in

( select max(number) from chengji group by number having count(scorenumber)=(select count(*) from score) )

9、查询有2门以上成绩的学生的姓名和联系方式。

select name,lianxidianhua from student where number in( select number from chengji group by number having count(scorenumber)>2)

10、查询哪些学生有c#考试成绩,显示这些学生的姓名。

select name from student where number in( select number from chengji where scorenumber =(select scorenumber from score where kemu='c#'))

给结果集添加编号

11.查询出科目中教师的编号,同时增加序号。

 select 序号=(select count(*) from score as a where a.scorenumber<=b.scorenumber ),teacherid from score as b

 select 序号=identity(1,1),teachername into #newtable from score

12.随机显示学生信息表中的两条数据:使用newid()函数

select top 2 * from student order by newid()

13.查询出联系电话为null的学生信息,联系电话用‘无’代替。

select name,isnull(convert(char(20),lianxidianhua),'无') from student

14.将查询出的地区为大连的都显示为null

select name,nullif(diqu,'大连') from student

15.查询科目平均成绩超过70分的教师姓名。

select teachername from teacherinfo where teacherid in

(select teacherid from score where scorenumber in

(select scorenumber from chengji group by scorenumber having avg(score)>=70))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐