SQL实验三
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验三嵌套查询和统计查询
一、实验目的
要求学生熟练使用T-SQL语句进行数据查询,掌握SELECT语句的基本结构和多表连接查询、子查询、分组查询、查询结果的排序等操作。
二、实验内容
.利用SELECT查询语句进行单表、多表查询设计。
.利用SELECT语句进行子查询和外连接查询。
.设计ORDER BY查询子句以及带有GROUP BY的查询子句。
三.实验环境
实验室名称:11#211
软件环境:操作系统:windows 10;SQL 2008
硬件环境:安装内存(RAM):4.00GB(3.13GB可用)
处理器:Intel Core(TM) i5-4200H CPU@2.80GHz 2.79GHz
硬盘:1TB
四、实验步骤
1. 嵌套查询
用TransacTransact-SQL语句表示,在学生选课库中实现其数据嵌套查询操作:
(l) 查询选修了“线性代数”的学生学号和姓名。
use JXGL
go
select sno,sname
from S
where sno in
(select sno
from sc
where cno=
(select cno
from c
where cname='线性代数'));
Go
图c1 实验3.1.1实验结果
(2)查询“c1”课程的成绩高于张苏皖的学生学号和成绩。
Use jxgl
go
select sno,grade
from sc
where cno='c1'and grade>
(
select grade
from sc
where cno='c1'and sno=(select sno
From s
Where sname='张苏皖'));
Go
图c2 实验3.1.2实验结果
(3) 查询其他系中年龄小于数理学院(MA)年龄最大者的学生。Use jxgl
go
select *
from s
where Sdept<>'ma' and age<(select max(age)
from s
where Sdept='ma');
Go
图c3 实验3.1.3实验结果
(4) 查询其他系中比数理学院学生年龄都小的学生。
Use jxgl
go
select *
from s
where Sdept<>'MA' and age<(select min(age)
from s
where Sdept='MA');
go
图c4 实验3.1.4实验结果
(5) 查询同“焦卫军”的“数据库”课程分数相同的学生的学号。
Use jxgl
go
select sno
from sc
where grade=(select grade
from S JOIN sc ON s.sno=sc.sno JOIN c ON
o=o and ame='数据库'and sname='焦卫军'); go
图c5 实验3.1.5实验结果
(6) 查询选修了‘c1’课程的学生姓名。
select sname
from s
where sno in(select sno
from sc
where cno='c1');
图c6 实验3.1.6实验结果
(7)查询没有选修‘c1’课程的学生姓名。
Use jxgl
go
select sname
from s
where sno NOT in(select sno
from sc
where cno='c1');
go
图c7 实验3.1.7实验结果
5. 组合查询和统计查询
在学生选课数据库中实现其查询操作:
(1) 查找选修“概率论”课程的学生成绩比此课程的平均成绩大的学生学号和成绩。
select x.sno,x.grade
From sc as x
Where x.grade>(
select avg(y.grade)
From sc as y,C as C
Where ame='概率论')and o=
(select cno
From C
Where cname='概率论');
图c8 实验3.5.1实验结果
(2) 查询选修概率论课程的学生的平均成绩。
Use jxgl
go
select avg(grade)
From sc
Where sno in
(select sno
From sc
Where cno=
(select cno
From c Where cname='概率论'));
go
图c9 实验3.5.2实验结果
(3) 查询年龄大于女同学平均年龄的男同学姓名和年龄。
Use jxgl
go
select sname,age
From s
Where sex='男'and age>
(select avg(age)
From s
Where sex='女');
go
图c10 实验3.5.3实验结果
(4) 列出各系学生的总人数,并按人数进行降序排列。
select sdept ,count(*)as total
From s