实验六 使用T-SQL语句查询数据
一.实验目的:
熟练掌握使用SELECT语句进行数据查询。
二.实验内容:(所有题写到实验报告中)
1.对数据库stuinfo使用T-SQL命令进行如下操作:
查询student表中的学号、姓名和年龄并为列设置别名,结果按学号升序排。
查询班级(要求不重复)。
查询姓“王”的学生信息。
查询成绩在80-100之间的学号、课程号及成绩,结果先按课程号升序排,课程号一样的再按成绩降序排。
查询所有缺考的学生的学号及课程号。
查询 ‘3-105’课的选课人数、最高分、最低分和平均分。
查询每位同学的平均成绩,包括学号和平均成绩两列,结果按学号升序排。
查询各班各门课的平均成绩,包括班号、课程号和平均成绩三列,结果先按班升序排,班一样的再按课程号升序排。
查询score表中至少有5名学生选修的课程号及平均分。
查询其平均分高于80的学生的学号,姓名和平均分。
查询“95031”班所选课程的平均分,包括课程名和平均分。
查询所有教师的任课情况,包括教师姓名和课程名两列,如果某位教师没有任课则课程名列显示NULL。
查询其最低分大于70,最高分小于90的学生的学号、所选课程号及其分数。
查询成绩高于所选课平均分的学生学号、姓名、课程号和成绩。
查询每门课最高分的课程名、学生姓名和成绩。
查询选修其课程的学生人数多于5人的教师姓名。
查询没选“张旭”教师课的学生成绩,并按成绩递增排列。
查询没有任课的教师的姓名。
查询没有选修"6-166"课程的学生的学号和姓名。
查询出所有男生信息放入NS表中。
删除没人选的课程。
将“95031”班学生的成绩全部减去10分。
\2. 对订单管理库ordermanagement使用T-SQL命令进行下列查询。
ordermanagement数据库中有三个表,其结构如下:(加下划线的为主键)
客户表customer(客户号,客户名,地址,电话)
订单表order_list(订单号,客户号,订购日期)
订单明细表Order_detail(订单号,器件号,器件名,单价,数量)
使用SELECT语句完成下列查询:
查询2001年的所有订单信息(订单号,客户号,订购日期)。
查询订单明细中有哪些器件(即查询不重复的器件号和器件名)。
查询客户名为“三益贸易公司”的订购单明细(订单号、器件号、器件名、单价和数量),
查询结果先按“订单号”升序排,同一订单的再按“单价”降序排。
查询目前没有订单的客户信息。
查询客户名中有“科技”字样的客户信息。
查询每笔订单的订单号和总金额,查询结果按订单号升序排,查询结果存入表ZJE中。
查询订单数量超过5笔的客户号及订单数量,查询结果按订单数量降序排。
查询每种器件中单价最低的订单明细(订单号、器件号、器件名、单价和数量)。
对表order_detail建立查询,把“订单号”的尾部字母相同且“器件号”相同的订单合并
成一张订单,新的“订单号”取原来订单号的尾部字母,器件号不变,“单价”取最低价,
“数量”取合计,查询结果先按新的“订单号”升序排,再按“器件号”升序排。
- 查询销售总数量最多的三种器件及其总数量。
use stuinfo
select sno as '学号',sname as '姓名',DateName(year,GetDate())-DATEPART(YYYY,sbirthday) as '年龄' from student order by sno asc;
select sno as '学号',sname as '姓名', DateName(year,GetDate())-DATEPART(YYYY,sbirthday) as '年龄' from student order by sno desc
---2
select distinct sclass from student
---3
select sname from student where substring(sname,1,1)='王'
---4
select sno,cno,degree from score where degree>=80 order by cno asc,degree desc
---5
select sno,cno from score where degree is null
---6
select COUNT(*) as '选课人数',MAX(degree) as 'max',MIN(degree) as 'min',AVG(degree) as 'avg' from score
---7
select sno,AVG(degree)as 'avg' from score group by sno order by sno desc
---8
select sclass,cno,AVG(degree) as 'AVG' from student,score group by sclass,cno order by sclass,cno
---9
select cno,AVG(degree) from score group by cno having COUNT(*) >= 5
---10
select student.sno as '学号', sname as '姓名' ,AVG(degree) as 'AVG' from student,score where student.sno=score.sno group by student.sno,student.sname having AVG(degree)>=80
---11
select course.cname as '课程名',AVG(degree) as '平均分' from student,course,score where student.sno=score.sno and sclass='95031' group by course.cno,course.cname
---12
select tname as '教师姓名',cname as '课程名' from teacher left join course on teacher.tno = course.tno group by tname,cname
---13
select sno as '学号',cno as '课程号',degree as '分数' from score a WHERE (SELECT MIN(degree) FROM score b where a.sno=b.sno)>70 and (SELECT MAX(degree)FROM score c where a.sno=c.sno)<90 and degree is not NUll
---14
select score.sno,sname,cno,degree from student,score where student.sno=score.sno and degree>(select AVG(degree) from score,course where score.cno=course.cno)
---15
select cname '课程名',sname '姓名',degree '成绩' from student,course,score where student.sno=score.sno and course.cno=score.cno and degree=(select MAX(degree)from score where score.cno=course.cno)
---16
select tname as 教师姓名 from teacher,course where teacher.tno=course.tno and (select COUNT(*) from score where score.cno=course.cno)>5
---17
select score.degree from score,teacher,course where teacher.tno=course.tno and course.cno=score.cno and tname!='张旭' order by degree
---18
select tname from teacher except select tname from teacher,course where teacher.tno=course.tno
---19
select student.sno,student.sname from student,course,score where score.sno not in (select sno from score where cno='1-166') group by student.sno,student.sname
---20
select student.sno as 学号,student.sname as 学生姓名,ssex as 性别,sbirthday as 生日,sclass as 班级,cname as 课程,degree as 成绩 into NS from student,score,course where student.sno=score.sno and ssex='男' and course.cno=score.cno order by student.sno
---21
delete course where course.cno not in (select cno from score)
---22
update score set degree = degree - 10 from student,score where student.sno=score.sno and student.sclass='95031'
第二题
use OrderManagement
---1
select * from order_list
---2
select distinct 器件号, 器件名 from order_detail
---3
select order_detail.订单号, order_detail.器件号, order_detail.器件名, order_detail.单价, order_detail.数量 from Customer, order_detail, order_list
where Customer.客户名 = '三益贸易公司' and Customer.客户号 = order_list.客户号 and order_list.订单号 = order_detail.订单号
order by 订单号, 单价 desc
---4
select * from Customer where Customer.客户号 not in (select 客户号 from order_list)
---5
select * from Customer where 客户名 like '%科技%'
---6
select 订单号, sum(单价 * 数量) as '总金额' into ZJE from order_detail group by 订单号 order by 订单号
---7
select 客户号, COUNT(订单号) from order_list group by 客户号 having count(订单号) > 5 order by COUNT(订单号) desc
---8
select * from order_detail a where 单价 in (select MIN(单价) from order_detail b where a.器件名=b.器件名)
---9
select RIGHT(订单号, 1) as 订单号, 器件号, MIN(单价)as 最低价,SUM(数量) as 数量 from order_detail group by 订单号, 器件号 order by 订单号, 器件号
---10
select top 3 器件号, 器件名, SUM(数量) as 数量 from order_detail group by 器件号, 器件名 order by 数量 desc
实验六 使用T-SQL语句查询数据相关推荐
- sap直接执行SQL语句查询数据
有时候需要临时在生产机查询部分数据,但通过SQVI实现不了,查询语句相对复杂,我们可以通过DBACOCKPIT这个事务码下的Performance–>Additional Functions–& ...
- B站黑马测试第一篇P182视频数据库SQL语句查询数据准备代码
drop table if exists goods; create table goods(id int unsigned primary key auto_increment,goodsName ...
- Excel 中使用SQL 语句查询数据(七)-----用LIKE 运算符进行模糊匹配查询
这篇博文要和大家分享的是用LIKE 运算符进行模糊匹配查询下图数据源商品代号包含数字的数据. 我们用Microsoft query连接数据源,步骤请参考本系列第一篇博文.语句如下图 其中 LIKE ' ...
- 数据库原理实验2:使用Select语句查询数据(一)——简单查询
1.找出机械工业出版社图书的书号,书名,作者,价格信息: select book_number,book_name,author,price from book where publisher = ' ...
- sql语句查询数据变成表格
SELECT TABLE_NAME 表名,COLUMN_NAME 列名,COLUMN_TYPE 数据类型,DATA_TYPE 字段类型,CHARACTER_MAXIMUM_LENGTH 长度,IS_N ...
- thinkphp5基本的一些操作/API友好/获取请求信息(Request)/判断请求类型(GET...)/验证参数数据(Validate)/连接数据库/原生sql语句查询
文章目录 一.API友好 1.举两个thinkphp5关于API友好的例子 (1)数据输出 (2)错误调试Trace 二.获取请求信息(Request) 1.获取URL信息 2.获取 模块/控制器/操 ...
- 如何用SQL语句查询Excel数据
Q:如何用SQL语句查询Excel数据? A:下列语句可在SQL SERVER中查询Excel工作表中的数据. 2007和2010版本: SELECT * FROM OpenDataSource( ...
- sql--sqlsever--时间相关SQL语句--查询当前时间至前N天的数据
sql–sqlsever–时间相关SQL语句–查询当前时间至前N天的数据 . . . sql server:取当前时间前10分钟之内的数据 dateadd() 当前时间 select GETDATE( ...
- sql语句查询出重复的数据
sql语句查询出一张表中的重复数据 问题描述: 未去重的数据,比去重之后的多了一条,现想获取该重复的数据 SELECT SheetID FROM anquan.finance GROUP BY She ...
- sql语句查询到上周、上个月的数据
sql语句查询到上周.上个月的数据 前言:先前我都是用程序去处理这个功能,但是后面觉得有点太麻烦,就干脆用sql来查询到上个月的数据 上个月: SELECT* FROMuser_lesson_deta ...
最新文章
- python学习干货教程(10):列表
- display:inline-block+text-align:justify实现列表元素的两端对齐
- 为什么(#39;b#39;+#39;a#39;+ +#39;a#39;+#39;a#39;)。toLowerCase()#39;banana#39;的结果?
- tensorflow安装教程
- android studio 显示方法列表,有没有办法在Android Studio编辑器中显示RecyclerView内容的预览?...
- iOS15实现音乐播放器
- 演练 课程导航 1002 html
- Web Application Framework
- IDEA springboot maven 项目部署
- c语言后置 运算符,98-递增运算符的前置和后置形式
- LINUX最好用查看端口占用并杀死(kill)的方式
- Python设计模式:建造者模式
- 挨踢部落故事汇(9):女程序媛的开发梦
- Loadrunner:管理员权限启动报错“win10为了对电脑进行保护,已经阻止此应用”
- Google新人的成长思考
- pybind11学习 | 在Python中构建编译生成pyd文件
- 清除DataGridView的全部内容,包括标题行
- 社会网络分析工具—— Gephi 或 NetworkX的简单介绍和比较(源自GPTchat)
- maven 打包把依赖jar打进去
- 继承——Person为父类,Teacher和Student都继承Person
热门文章
- 程序员每天累成狗,是为了什么
- windows “你尚未连接代理服务器可能有问题,或地址不正确“ 解决方案
- python量化期权_如何20小时搞定Python量化期权实战?
- AB32实例应用(4.非常规经验及技巧)
- 孙陶然:切合实际是设定目标的基础
- 信息论:数据压缩和信源编码
- BAT批量替换文件内容
- python 复制替换文件_在Python中复制和替换文件
- vue打包时报错 Error: No PostCSS Config found in 的解决方法
- Python核心编程16 ----- 文件的打开(读取),修改,关闭,二进制