实验3  数据检索

一、实验目的

(1)理解T-SQL语言的使用。

(2)熟练掌握数据查询语句。

SELECT [ALL|DISTINCT] {*|<表达式>,…,<表达式>}

FROM <表名>[, <表名>…]

[WHERE <条件>]

[GROUP BY <列名>[, <列名>…][HAVING <谓词>]]

[ORDER BY <列名> [ASC|DESC] [, <列名> ASC|DESC]…]

[COMPUTE …]

二、实验内容

1[3.2]b  USE CAP
select aid from AGENTS where [percent]=(select max([percent] )from AGENTS
[3.5]  Select cid,aid from customers c, agents a  Where cid not in (select cid from orders x
Where x.cid = c.cid and x.aid = a.aid)  And
aid not in (select aid from orders v  Where v.aid = a.aid and v.cid = c.cid)
[3.8]a   select cid,max(dollars) as MAXSPEND from orders group by cid
[3.8]b   select avg(MAXSPEND) average,cid from  (select cid,max(dollars) as MAXSPEND from orders group by cid)as S group by cid
[3.11]b  Select avg(temp.maxspend)avgspend,temp.cid
from (select cid,max(dollars) as maxspend from orders  
Group by cid) as temp group by temp.cid
[3.11]f  select pid from products p
where not exists(select cid from customers c where city='Duluth'
and not exists (select * from orders o where o.cid=c.cid and o.pid=p.pid))
[3.11]j  update products
set price=price*1.1 where city='Duluth' or city='Dallas'
[3.11]l  select aid,[percent] from agents a where not exists(select * from customers c
where city='Duluth' and not exists
(select * from orders o where o.cid=c.cid  and o.aid=a.aid)) order by [percent]DESC

<选做>[3.15]、[3.16]、[3.17]

2、在“学生管理数据库”中建立四张表,分别如下:

学生表:(主键:学号)

学号

姓名

性别

年龄

所在院系

班级

入学日期

20009001

葛文卿

22

国际贸易

国贸2班

2000-8-29

20014019

郑秀丽

21

会计学

会计1班

2001-9-2

20023001

刘成铠

18

计算机

软件2班

2002-8-27

20026002

李涛

19

电子学

电子1班

2002-8-27

20023002

沈香娜

18

计算机

软件2班

2002-8-27

20026003

李涛

19

计算机

软件1班

2002-8-27

20023003

肖一竹

19

计算机

软件2班

2002-8-27

课程表:(主键:课程号)

课程号

课程名

先修课

学分

C801

高等数学

4

C802

C++语言

C807

3

C803

数据结构

C802

4

C804

数据库原理

C803

4

C805

操作系统

C807

4

C806

编译原理

C803

4

C807

离散数学

4

成绩表:(主键:学号、课程号)

学号

课程号

成绩

学分

20023001

C801

98

4

20023002

C804

70

4

20026001

C801

85

4

20023001

C802

99

3

20026002

C803

82

4

授课表:(主键:课程号、班级名)

教师名

课程号

学时数

班级名

苏亚步

C801

72

软件2班

王文山

C802

64

软件2班

张珊

C803

72

软件2班

王文山

C804

64

软件2班

苏亚步

C801

72

软件1班

1)列出软件2班女同学的名单

select * from 学生表 where 性别='女'  and 班级='软件2班'
2)列出2002年元旦前注册的学生名单
select 姓名 ,入学日期 from 学生表  where 入学日期 < ' 2002-1-1 ';
3)列出所有年龄在19岁以下,或者女同学的名单

select 姓名,性别,年龄 from  学生表 where 年龄<19 or  性别='女'
4)列出没有先修课的课程名
select 课程名,先修课 from 课程表  where 先修课 is null
5)列出既不是“电子系”,也不是“会计系”的学生的学号、姓名和所在院系

select 学号,姓名,所在院系 from 学生表
where 所在院系<>'会计学' and 所在院系<>'计算机'
6)查询选修“C 801 ” 课程的学生的最高分

select 课程号,max(成绩) 最高分 from 成绩表  where 课程号='C801' group by 课程号
7)统计男、女学生各自的人数和平均年龄

select 性别,count(学生表.学号) 个数,avg(年龄) 平均年龄 
from 学生表,成绩表 group by 性别
8)列出选修了一门以上课程的学生学号,及其选修门数

select C1.学号,count(C1.课程号)选课门数 from 成绩表 C1,成绩表 C2
where C1.学号=C2.学号 AND C1.课程号<>C2.课程号  GROUP BY C1.学号
9)查询“沈香娜”同学所在班级的所有学生的学号和姓名

select 学号,姓名 ,班级 from 学生表 
where 班级 in(select 班级 from 学生表 where 姓名='沈香娜')
10)统计每一年龄选修课程的学生人数

SELECT 年龄,COUNT(课程号) 选课门数 FROM 学生表,成绩表
WHERE 学生表.学号=成绩表.学号  GROUP BY 年龄

难题:

1)        在基本表“成绩表”中检索平均成绩最高的学生学号

select C.学号 from(select avg(成绩) 平均分,学号 from 成绩表 group by 学号) C 
where C.平均分>=all(select 平均分 from
(select avg(成绩) 平均分,学号 from 成绩表 group by 学号) C  )

2)        求表S中男同学的每一年龄组(超过2人)有多少人?要求查询结果按人数升序排列,人数相同按年龄降序排列。
select 年龄 as 男生年龄,count(年龄) 个数 from 学生表 where 性别='男'
group by 年龄 having count(年龄)>=2

3)        列出选修了全部课程的学生学号和姓名
 
select s.学号,s.姓名 from 学生表 s  where not exists (select * from 课程表 c  where not exists (select * from 成绩表 a where a.学号 = s.学号 and a.课程号 = c.课程号));

4)        查询这样的学生,该生至少选修了学生20026001所选修的全部课程
select x.* from 成绩表 x, 成绩表 y  where x.课程号=y.课程号 and y.学号='20026001' and X.学号<>' 20026001’

 

四、实验结果

3.2
USE CAP
select aid from AGENTS where [percent]=(select max([percent] )from AGENTS)

3.5
Select cid,aid from customers c, agents a  Where cid not in (select cid from orders x
Where x.cid = c.cid and x.aid = a.aid)  And
aid not in (select aid from orders v  Where v.aid = a.aid and v.cid = c.cid)

3.8 (a)  select cid,max(dollars) as MAXSPEND from orders group by cid

(b) select avg(MAXSPEND) average,cid from  (select cid,max(dollars) as MAXSPEND from orders group by cid)as S group by cid

3.11(b) Select avg(temp.maxspend)avgspend,temp.cid
from (select cid,max(dollars) as maxspend from orders
Group by cid) as temp group by temp.cid

(f) select pid from products p
where not exists(select cid from customers c where city=' Duluth '
and not exists  (select * from orders o where o.cid=c.cid and o.pid=p.pid))

(j) update products  set price=price*1.1 where city=' Duluth ' or city=' Dallas '

(l) select aid,[percent] from agents a  where not exists(select * from customers c
where city=' Duluth ' and not exists
(select * from orders o where o.cid=c.cid  and o.aid=a.aid)) order by [percent]DESC

2.
1)use student
select * from 学生表 where 性别='女'

2)select *from

3) select 姓名,性别,年龄 from  学生表 where 年龄<19 or  性别='女'

4) select 课程名,先修课 from 课程表  where 先修课 is null

5) select 学号,姓名,所在院系 from 学生表
where 所在院系<>'会计学' and 所在院系<>'计算机'

6) select 课程号,max(成绩) 最高分 from 成绩表  where 课程号='C801' group by 课程号

7) select 性别,count(学生表.学号) 个数,avg(年龄) 平均年龄 
from 学生表,成绩表 group by 性别

8) select C1.学号,count(C1.课程号)选课门数 from 成绩表 C1,成绩表 C2
where C1.学号=C2.学号 AND C1.课程号<>C2.课程号  GROUP BY C1.学号

9) select 学号,姓名 ,班级 from 学生表 
where 班级 in(select 班级 from 学生表 where 姓名='沈香娜')

10) SELECT 年龄,COUNT(课程号) 选课门数 FROM 学生表,成绩表
WHERE 学生表.学号=成绩表.学号  GROUP BY 年龄

11) select C.学号 from(select avg(成绩) 平均分,学号 from 成绩表 group by 学号) C 
where C.平均分>=all(select 平均分 from
(select avg(成绩) 平均分,学号 from 成绩表 group by 学号) C  )

12)select 年龄 as 男生年龄,count(年龄) 个数 from 学生表 where 性别='男'
group by 年龄 having count(年龄)>=2

13)select s.学号,s.姓名 from 学生表 s  where not exists (select * from 课程表 c
where not exists (select * from 成绩表 a where a.学号 = s.学号 and a.课程号 = c.课程号));

14)select X.* from 成绩表 x, 成绩表 y  where x.课程号=y.课程号 and y.学号='20026001' and X.学号<>'20026001

学生表where 入学期 < ' 2002-1-1 ';

SQL SERVER实验-- 数据检索相关推荐

  1. SQL server实验练习1

    SQL server实验练习1 (1)请根据下面描述,使用SQL语句完成数据库的创建 名称:stuDatabase 主数据文件: 逻辑文件名: Data_M 物理文件名:stuDatabase__M. ...

  2. SQL Server 实验二 创建数据库

    SQL Server 实验二 创建数据库 这是一个系列,需几个文档一起看 Use master go Drop Database YGGLcreate database YGGL on (name=' ...

  3. SQL Server实验——数据库视图的定义与使用

    SQL Server实验--数据库视图的定义与使用 学生选课数据库的创建 创建学生信息表student 创建课程信息表course 创建学生选课信息表sc 插入相关信息 视图的创建 基本应用 一.创建 ...

  4. SQL server 实验练习2

    SQL server 实验练习2 1.根据要求使用DML完成数据编辑 (1)在表中添加1个新的系,dno为"d01006",并为该系添加2个专业信息和2条教师信息,其他具体数据自拟 ...

  5. SQL Server 实验五 T-SQL编程

    SQL Server 实验五 T-SQL编程 这是一个系列,需几个文档一起看 Use YGGL--2.变量使用 --(1) Declare @female bit Set @female=0 Sele ...

  6. SQL Server实验

    SQL Server实验 一.实验目的 二.实验步骤 三.实验内容 四.实验总结 一.实验目的 在SQL Server中创建数据库的基础上创建表,然后对表数据进行增删改操作. 二.实验步骤 (一)安装 ...

  7. 2018年11月16日SQL Server实验内容(触发器实验)

    --注意:先把studentmanager数据库中的所有表用select into命令复制一份, --然后用复制后新表完成下面的实验,同时,对每个触发器都要进行验证. select *into dep ...

  8. 上海师范大学计算机作业网址,新SQL——SERVER实验练习答案.doc

    SQL-Server实验讲义 PAGE 30 SQL-Server实验 答案 上海师范大学 计算机系 目 录 TOC \o "1-3" \h \z \u HYPERLINK \l ...

  9. SQL server 实验五 (sql 查询语句)

    实验五 实验知识储备: 一.当做一个查询题时 (1)首先要审题,明确你的查询对象 即select 后面的内容 (2)明确你的查询对象分别来自于哪一个表 即from 后面的内容 (3)找出查询条件/约束 ...

最新文章

  1. 研发投入超876亿的华为,将如何进击云+AI?
  2. JGG: 赵方庆、赵翔等开发高效捕获RNA病毒基因组的宏转录组建库测序新技术
  3. 【基础】使用cookies,实现免登陆(七)
  4. 为炒股每天只花3元 MM从贷款上学到掌控千万
  5. LeetCode 1310. 子数组异或查询(前缀异或)
  6. Form Template Method(塑造模板函数)
  7. 昔年浅谈做害虫消杀防护的用什么推广效果好?
  8. 记某站被搜索引擎入侵
  9. 恩布企业 IM 安卓端 1.1 增强版本
  10. java接口测试工具_接口模拟测试利器,moco server工具的介绍
  11. roc曲线怎么绘制_Excel怎么绘制曲线图?Excel绘制曲线图教程
  12. 7个实用的Python自动化代码,别再重复造轮子了
  13. 汽车行业数字化工厂建设方案
  14. flac转mp3,flac格式如何转mp3
  15. python+vue+Elementui植物园网站的设计django
  16. 天图投资境外上市获批:上半年营收降​18% 投了小红书与百果园
  17. word文档合并的几种方式
  18. STM32CubeMX 下载和安装 详细教程
  19. 分布式链路追踪在字节跳动的实践
  20. sklearn模型中random_state参数的意义

热门文章

  1. Qt窗口之间传值和结构体数据
  2. 精品软件 推荐 酷我音乐 一个可以下载320k 音质的音乐播放软件
  3. JAVA--Socket【“套接字”】
  4. arm工控主板,三星S5P4418嵌入式开发板
  5. hook C++类的虚表,替换成其他函数
  6. VSCode搭建 c 环境教程
  7. 权限管理系统(用户信息管理模块业务组件实现代码,带注解)
  8. RabbitMQ和fegin补充
  9. 角位移/倾角/角度传感器如何安装
  10. java split 双竖线_HIVE 常用函数及实例