首页 > 数据库 >SQL SERVER实验-- 数据检索

SQL SERVER实验-- 数据检索

时间:2023-06-09 10:32:01浏览次数:45  
标签:成绩表 cid 数据检索 学号 SERVER -- 课程 where select


实验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 姓名,性别,年龄  学生表 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 学生表  where not exists (select * from 课程表 where not exists (select * from 成绩表 a where a.学号 = s.学号 and a.课程号 = c.课程号));

4)        查询这样的学生,该生至少选修了学生20026001所选修的全部课程
select x.* from 成绩表 x, 成绩表  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 姓名,性别,年龄  学生表 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 学生表  where not exists (select * from 课程表 c
where not exists (select * from 成绩表 a where a.学号 = s.学号 and a.课程号 = c.课程号));

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


 

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

标签:成绩表,cid,数据检索,学号,SERVER,--,课程,where,select
From: https://blog.51cto.com/u_16129500/6445749

相关文章

  • 菜鸟攻略–C语言多文件编程初探(二):使用 gcc 手动编译多文件 C 程序
    step1:下载安装Dev-C++已经安装了Dev-C++或系统中的可以跳过这步。去官网下载Dev-C++。我昨天下载,发现有点慢,所以我把安装文件放到百度网盘了,供大家下载,下载链接为:http://pan.baidu.com/s/1pLPenDx。开始安装,记住安装位置。在安装时只能选择英文,安装完成后,第一次启动时可以选择......
  • Windows证书管理器 && SSL certification && WSL-Docker: curl: (60) SSL certificat
    深入浅出certmgr——Windows证书管理器https://www.fke6.com/html/91605.html计算机安全是当前社会的一个重要议题,证书是一种重要的安全机制,负责证明数据、软件或者人的身份和信誉。certmgr(即“证书管理器”)是Windows中专门用于证书管理的工具。本文将从多个方面对certmgr进行深......
  • CoreDNS搭建内网DNS服务
     蚂蚁背大象2022年02月16日23:57 ·  阅读1502「这是我参与2022首次更文挑战的第30天,活动详情查看:2022首次更文挑战」1.背景因为自己有在开发一个聊天的服务,注册中心设计参考了RocketMQ的NameServer。注册中心都是单独部署,注册中心和注册中心之间没有数据交互。......
  • 010 数据库学习笔记 日期 + 时间 + 星期
    星期获取操作--设置语言,这个很重要,会导致不同的返回结果(只需设置一次语言后,就可以重复调用DATENAME)SETLANGUAGEN'SimplifiedChinese'--简体中文--SETLANGUAGEN'English'--英文--SETLANGUAGEN'Korean'--韩文--获取指定日期为星期几GETDATE()当天日期,GETDATE(......
  • CSS 图片加载提前占位 padding-top、padding-bottom
    今天聊一个图片加载提前占位的一个问题......
  • dnsmasq( DNS和DHCP)服务
    目录一、简介二、dnsmasq安装三、dnsmasq的解析流程四、dnsmasq的作用以及相关配置五、DHCP服务器设置 一、简介Dnsmasq提供DNS缓存和DHCP服务功能。作为域名解析服务器(DNS),dnsmasq可以通过缓存DNS请求来提高对访问过的网址的连接速度。作为DHCP服务器......
  • quickfix协议当有中文时校验位错误问题解决
    quickfix校验位计算都是根据ISO-8859-1编码计算,知道这个规则后续我们处理中文就很好处理了。但是如果用ISO-8859-1编码有中文会出现乱码,如果将CharsetSupport.setCharset设置为UTF-8或者GBK时,在发送数据时会报java.nio.bufferoverflowexception:null,或者校验位失败。1、往step网......
  • 【技术积累】算法中的贪心算法【二】
    如何证明一个问题可以使用贪心算法解决?判断一个问题是否可以使用贪心算法解决,通常需要满足两个条件:贪心选择性质:问题的最优解可以通过一系列局部最优解得到。也就是说,在每一步选择中,都选择当前最优解,而不考虑之后的影响。最优子结构性质:问题的子问题的最优解可以推导出原问题......
  • ai问答:Vue3中使用computed和watch监听pinia数据的区别
    在Vue3中,通过computed来监听pinia中的state数据:import{defineStore}from'pinia'import{useStore}from'pinia'import{computed}from'vue'conststore=defineStore('store',{/*...*/})exportdefault{setup(){......
  • OpenSCAP的基础使用方法
    大背景信息安全态势日趋严重,对我们网络安全工作提出了更高的要求,遵循一定的网络安全框架的同时,我们需要在各个不同的环节部署不同的安全措施形成纵深防御体系。服务器侧的安全基线在安全管理中是一个基础性的工作,是整个体系中不可缺少的一环,但是也是一个比较麻烦的工作,一方面是......