6Transact-SQL语句的高级应用

  1. 掌握多表连接查询、子查询、游标、大对象类型数据的基本概念。
  2. 掌握多表连接的各种方法,包括内连接、外链接和交叉连接等。
  3. 掌握子查询的方法,包括相关子查询和不相关子查询。
  4. 掌握游标处理结果集的基本过程。

1.查询所有班级的期末成绩平均分,并按照平均分降序排序。

select classno,avg(final) from student,score where student.studentno=score.studentno group by classno order by avg(final) desc

2.查询教师基本信息和教授课程信息,其中包括未分配课程的教师信息。

select * from teacher left join teach_class on teacher.teachno=teach_class.teacherno 

3.查询160501班级中选修了“韩晋升”老师讲授的课程的学生学号、姓名、课程号和期末成绩。

 select student.studentno,sname,courseno,final from student,score 
where student.studentno=score.studentno and classno=160501 and
courseno=(select courseno from teach_class where teacherno=(select teachno from teacher where tname='韩晋升'))

4.查询每门课程的课程号、课程名和选修该课程的学生人数,并按所选人数升序排序。

select score.courseno,COUNT(*) as 选课人数  from score inner join course on course.courseno=score.courseno group by score.courseno order by COUNT(studentno) desc

5.查询两门及以上课程的期末成绩超过80分的学生姓名及平均成绩。

 select student.sname,AVG(final) as '平均成绩' from student, score where final>80 and student.studentno=score.studentno group by student.studentno,student.sname having COUNT(*)>=2

6.查询入学考试成绩最高的学生学号、姓名和入学成绩。

select top 1 sname,studentno,point from student order by point desc

7.查询同时教授c05127号和c05109号课程的教师信息。

select * from teacher left join teach_class on teacher.teachno=teach_class.teacherno where courseno='c05127' and courseno='c05109'

8.查询至少选修了姓名为“韩吟秋”的学生所选修课程中一门课程的学生学号和姓名。

declare @a nchar(10)
select @a=(select studentno from student where sname='韩吟秋')
select studentno,sname from student where studentno in (select studentno from score where courseno in (select courseno from score where studentno=@a))

9.查询所有教授c05127号课程的教师信息。

select * from teacher where teachno in (select teacherno from teach_class where courseno='c05127')

10.查询没有被任何学生选修的课程编号、课程名称和学分。

select course.courseno,cname,credit,case when score.courseno is null then '没选课' else score.courseno end from course left join score on course.courseno=score.courseno

11.查询“C语言”课程期末成绩比“电子技术”课程期末成绩高的所有学生的学号和姓名。

select a.studentno,a.sname from
(select student.studentno,sname,final from student,score,course where 
student.studentno=score.studentno and score.courseno=course.courseno and course.cname=N'C语言') as a,
(select student.studentno,sname,final from student,score,course where
student.studentno=score.studentno and score.courseno=course.courseno and course.cname=N'电子技术') as b
where a.final>b.final and a.studentno=b.studentno

12.查询所有班级期末平均成绩的最高分,并将其赋值给变量,通过PRINT语句输出。

declare @max float
set @max=(select top 1 a.max from(select AVG(final) as 'max' from student,score,course 
where student.studentno=score.studentno and score.courseno=course.courseno 
group by student.classno)as a)
print cast(@max as varchar(30))
select @max

13.使用游标输出学生姓名、选修课程名称和期末考试成绩。

declare @sname nchar(8),@cname nchar(10),@final numeric(6,2)
declare sc cursor
for
select sname,cname,final from score join student on
(score.studentno=student.studentno)join course on(score.courseno=course.courseno)
open sc
fetch next from sc into @sname,@cname,@final
print '学生姓名  课程名称  期末成绩'
print '------------------------------'
while @@FETCH_STATUS=0
begin
      print @sname+@cname+cast(@final as nchar(6))
      fetch next from sc into @sname,@cname,@final
end
close sc
deallocate sc

14.使用游标统计每个学院教师所开设课程的选修率。

declare @a nchar(30),@b int,@avg float
declare cur cursor static
for 
    select department,count(*) as '选修课数' from class where class.classno in
    (select student.classno from student group by classno)
    group by department
open cur
fetch cur into @a,@b
set @avg=@b/(select COUNT(*) from class where department=@a)
print @avg
while @@FETCH_STATUS=0
begin
       fetch next from cur into @a,@b
       set @avg=@b/(select COUNT(*) from class where department=@a)
       print @a
       print @avg
end
close cur
deallocate cur

15.使用游标计算学生期末成绩的等级,并更新level列。

declare @sname nchar(30),@cname nchar(30),@final float
declare stu cursor static
for
    select sname,final,cname from student,score,course where
    student.studentno=score.studentno and course.courseno=score.courseno
open stu
fetch stu into @sname,@final,@cname
if @final>=90  print N'优秀'+@sname+@cname
else if @final>=80 and @final<90 print N'良好'+@sname+@cname
else if @final>=70 and @final<80 print N'中等'+@sname+@cname
else if @final>=60 and @final<70 print N'及格'+@sname+@cname
else if @final>=60 print N'差等'+@sname+@cname
while @@FETCH_STATUS=0
begin
       fetch next from stu into @sname,@final,@cname
if @final>=90  print N'优秀'+@sname+@cname
else if @final>=80 and @final<90 print N'良好'+@sname+@cname
else if @final>=70 and @final<80 print N'中等'+@sname+@cname
else if @final>=60 and @final<70 print N'及格'+@sname+@cname
else if @final>=60 print N'差等'+@sname+@cname
end
close stu
deallocate stu

你可能感兴趣的:(数据库)