实验:数据库综合查询.doc
文本预览下载声明
实验:数据库综合查询
一、实验目的
掌握SELECT语句的基本语法和查询条件表示方法;
掌握查询条件种类和表示方法;
掌握连接查询的表示及使用;
掌握嵌套查询的表示及使用;
了解集合查询的表示及使用。
二、实验环境
已安装SQL Server 2005 企业版的计算机;
具有局域网环境,有固定IP;
三、实验学时
2学时
四、实验要求
了解SELECT语句的基本语法格式和执行方法;
了解连接查询的表示及使用;
了解嵌套查询的表示及使用;
了解集合查询的表示及使用;
完成实验报告;
五、实验内容及步骤
以《实验:数据库单表查询数据》为基础,请使用T-SQL 语句实现进行以下操作:
查询以‘DB_’开头,且倒数第3个字符为‘s’的课程的详细情况;
use stu
go
select * from course
where cname like DB%s__
go
查询名字中第2个字为‘阳’的学生姓名和学号及选修的课程号、课程名;
use stu
go
select sname as 姓名,student.sno as 学号,sdept as 院系,
cno as 课程号,grade as 成绩
from student,sc
where student.sno=sc.sno and student.sname like _阳%
go
列出选修了‘数学’或者‘大学英语’的学生学号、姓名、所在院系、选修课程号及成绩;
use stu
go
select student.sno as 学号,sname as 姓名,sdept as 院系,
course.cno as 课程号,grade as 成绩
from student,sc,course
where student.sno=sc.sno and sc.cno=course.cno and
course.cname in (数学,大学英语)
go
查询缺少成绩的所有学生的详细情况;
use stu
go
select student.sno as 学号,sname as 姓名,ssex as 性别,
sage as 年龄,sdept as 院系
from student,sc
where student.sno=sc.sno and sc.grade is null
go
查询与‘张力’(假设姓名唯一)年龄不同的所有学生的信息;
use stu
go
select student.sno as 学号,sname as 姓名,ssex as 性别,
sage as 年龄,sdept as 院系
from student
where sage not in(select sage from student where sname=张力)
go
查询所选课程的平均成绩大于张力的平均成绩的学生学号、姓名及平均成绩;
use stu
go
select student.sno as 学号,sname as 姓名,
avg(grade) as 平均成绩
from student,sc
group by student.sno,sc.sno,student.sname
having student.sno=sc.sno and avg(grade)(select avg(grade)
from sc,student where student.sno=sc.sno and
student.sname=张力)
go
按照“学号,姓名,所在院系,已修学分”的顺序列出学生学分的获得情况。其中已修学分为考试已经及格的课程学分之和;
use stu
go
select student.sno as 学号,sname as 姓名,
sdept as 所在院系,sum(case when grade60 then ccredit
else 0 end)
as 已修学分
from student,course,sc
where student.sno=sc.sno and sc.cno=course.cno
group by student.sno,student.sname,student.sdept
go
列出只选修一门课程的学生的学号、姓名、院系及成绩;
use stu
go
select sc.sno as 学号,sname as 姓名,
sdept as 院系,grade as 成绩
from student,sc
where student.sno=sc.sno and
sc.sno in (
显示全部