哈工大数据库系统 实验:练习并熟练掌握交互式 SQL 语言

 
  1. 实验目的:基于给定的 OrderDB 数据库, 练习并熟练掌握交互式 SQL 语言
    实验环境:sql sever 2008

  1. 附:OrderDB 表结构及表间的关系

  1.  
  2. /* 1 查询职工工资按高低排序的前20%的职工编号、职工姓名和工资。 ok */
  3. select top 20 percent employeeNo , employeeName , salary
  4. from Employee
  5. order by salary DESC
  6.  
  7. /* 2 查询 业务科 或 财务科 的 职工姓名、性别和所在部门 ,仅显示前面5位职工。 ok */
  8. select top 5 employeeName , gender , department from Employee
  9. where department = '业务科' or department = '财务科'
  10.  
  11. /* 3 查询1973年9月份出生且为职员的员工编号、姓名、出生日期以及所在部门,并按出生日期的降序输出。ok */
  12. select employeeNo , employeeName , birthday , department from Employee
  13. where year(birthday) = 1973 and month(birthday) = 9 and headShip = '职员'
  14. order by birthday desc
  15.  
  16. /* 4 查询所有姓张的职工姓名、部门和性别(要求性别以中文显示)。ok */
  17. select employeeName , department ,
  18. case when gender = 'M' then '男' when gender = 'F' then '女'
  19. end gender from Employee
  20. where employeeName like '张%'
  21.  
  22. /* 5 查询姓张且全名为两个字的职工姓名。ok */
  23. select employeeName from Employee
  24. where employeeName like '张_'
  25.  
  26. /* 6 查询不同性别员工的人数 ok */
  27. select
  28. case when gender = 'M' then '男' when gender = 'F' then '女'
  29. end gender, count(gender) as '數目' from Employee
  30. group by gender
  31.  
  32. /* 7 查询全体职工的姓名、年龄、所属部门,并且用汉语显示表头信息。 ok */
  33. select employeeName 姓名 ,datediff(year,Birthday,getdate()) 年龄, department 所属部门
  34. from Employee
  35.  
  36. /* 8 查询工资在3000以下,3000至5000,以及5000以上的员工人数 ok */
  37. select
  38. (
  39. case when salary<3000 then '3000以下'
  40. when salary>=3000 and salary<=5000 then '3000至5000'
  41. when salary>5000 then '5000以上'
  42. end
  43. ) 工资 , count(*) as 员工人数
  44. from Employee
  45. group by
  46. case when salary<3000 then '3000以下'
  47. when salary>=3000 and salary<=5000 then '3000至5000'
  48. when salary>5000 then '5000以上'
  49. end
  50.  
  51. /* 9 查询薪水在2000到3000之间的职工编号、姓名、所在部门以及薪水。ok */
  52. /* 职工编号 姓名 所在部门 薪水 */
  53. select employeeNo , employeeName , department , salary from Employee
  54. where salary between 2000 and 3000
  55.  
  56. /* 10 查询1991年被雇佣的职工号、姓名、性别、电话号码、出生日期以及年龄
  57. (如果电话号码为空,显示“未知”,出生日期按yyyy-mm-dd显示)。ok */
  58. /* 职工号 姓名 性别 电话号码 出生日期 年龄 */
  59. select employeeNo , employeeName , gender , isnull(telephone,'未知') as 'telephone' ,
  60. convert(char(10),birthday,120) as 出生日期 ,
  61. datediff(year,Birthday,getdate()) as 年龄 from Employee
  62. where year(hireDate) = 1991
  63.  
  64. /* 11 查询每个客户的订单编号、客户名称、订单金额。ok */
  65. select o.orderNo , c.customerName , o.orderSum from OrderMaster o , Customer c
  66. where c.customerNo = o.customerNo
  67.  
  68. /* 12 查询住址在上海的员工所做的订单,输出员工编号、姓名、住址、订单编号、客户编号
  69. 和订单日期,并按客户编号排序输出。ok */
  70. /* 员工编号 姓名 住址 订单编号 客户编号 订单日期 */
  71. select e.employeeNo , e.employeeName , e.address , o.orderNo , o.customerNo , o.orderDate
  72. from Employee e , OrderMaster o
  73. where e.address like '上海%' and e.employeeNo = o.employeeNo
  74. order by o.customerNo
  75.  
  76. /* 13 查找与“吴浮萍”不在同一个部门工作的员工姓名、所属部门、性别和出生日期,并按所属部门排序输出。ok */
  77. /* 员工姓名 所属部门 性别 出生日期 */
  78. select employeeName , department , gender , birthday from Employee
  79. where department not in(
  80. select department from Employee
  81. where employeeName = '吴浮萍'
  82. )
  83. order by department
  84.  
  85. /* 14 查找订购了“32M DRAM”的商品的客户编号、客户名称、订单编号、订货数量和订货金额,
  86. 并按客户编号排序输出 ok */
  87. /* 客户编号 客户名称 订单编号 订货数量 订货金额 */
  88. select om.customerNo , c.customerName , od.orderNo , od.quantity , od.price*od.quantity 订货金额
  89. from OrderDetail od , OrderMaster om , Customer c , Product p
  90. where om.customerNo = c.customerNo and om.orderNo = od.orderNo and od.productNo = p.productNo
  91. and p.productName = '32M DRAM'
  92. order by om.customerNo
  93.  
  94. /* 15 查询销售总量大于4的商品编号、商品名称以及销售数量。 ok */
  95. /* 商品编号 商品名称 销售数量 */
  96. select P.productNo , P.productName , X.sales as '销售数量' from Product P , (
  97. select Y.productNo , Y.sales from(
  98. select OD.productNo , sum(OD.quantity) as sales from OrderDetail OD
  99. group by OD.productNo
  100. )Y
  101. where Y.sales > 4
  102. )X
  103. where X.productNo = P.productNo
  104.  
  105. /* 16 查询员工“张小娟”所做的 订单编号,客户名称,订单总金额以及发票号。 ok */
  106. /* 订单编号 客户名称 订单总金额 发票号 */
  107. select om.orderNo , c.customerName , om.orderSum , om.invoiceNo
  108. from OrderMaster om , Customer c , Employee e
  109. where om.employeeNo = e.employeeNo and c.customerNo = om.customerNo and e.employeeName = '张小娟'
  110.  
  111. /* 17 查询没有订购商品的且在北京地区的客户编号、客户名称和邮政编码,并按邮政编码降序排序。ok */
  112. /* 客户编号 客户名称 邮政编码 */
  113. select customerNo , customerName , zip from Customer
  114. where address = '北京市' and customerNo not in(
  115. select customerNo from OrderMaster
  116. )
  117. order by zip DESC
  118.  
  119. /* 18 查询订购了“32M DRAM”商品的订单编号、订货数量和订货单价。 ok */
  120. /* 订单编号 订货数量 订货单价 */
  121. select od.orderNo , od.quantity , od.price from OrderDetail od , Product p
  122. where p.productNo = od.productNo and p.productName = '32M DRAM'
  123.  
  124. /* 19 查询与员工编号E2008005在同一部门的员工编号、姓名、性别、所属部门。ok */
  125. /* 员工编号 姓名 性别 所属部门 */
  126. select employeeNo , employeeName , gender , department from Employee
  127. where employeeNo <> 'E2008005' and department in(
  128. select department from Employee where employeeNo = 'E2008005'
  129. )
  130.  
  131. /* 20 查询既订购了P20050001又订购了P20050003商品的客户编号及订单号。 */
  132. /* 按照不同的理解,这条语句应该有两种答案 */
  133. /* 客户编号 订单号 */ /* 同一个客户在同一份订单中购买了两份商品 */
  134. select OM.customerNo , OD.orderNo from OrderDetail OD , OrderMaster OM
  135. where OM.orderNo = OD.orderNo and OD.productNo = 'P20050001'
  136. and OM.customerNo in(
  137. select OM_1.customerNo from OrderDetail OD_1 , OrderMaster OM_1
  138. where OD_1.orderNo = OM_1.orderNo and OD_1.orderNo = OD.orderNo
  139. and OD_1.productNo = 'P20050003'
  140. )
  141.  
  142. /* 客户编号 订单号 */ /* 同一个客户允许在不同订单中购买了该两件商品 */
  143. select OM_1.customerNo , OM_1.orderNo
  144. from OrderMaster OM_1 , OrderDetail OD_1 , OrderMaster OM_2 , OrderDetail OD_2
  145. where OD_1.orderNo = OM_1.orderNo and OD_2.orderNo = OM_2.orderNo and OM_1.customerNo = OM_2.customerNo
  146. and OD_1.productNo = 'P20050001' and OD_2.productNo = 'P20050003'
  147. union
  148. select OM_1.customerNo , OM_1.orderNo
  149. from OrderMaster OM_1 , OrderDetail OD_1 , OrderMaster OM_2 , OrderDetail OD_2
  150. where OD_1.orderNo = OM_1.orderNo and OD_2.orderNo = OM_2.orderNo and OM_1.customerNo = OM_2.customerNo
  151. and OD_1.productNo = 'P20050003' and OD_2.productNo = 'P20050001'
  152.  
  153. /* 21 查询没有订购“52倍速光驱”或“17寸显示器”的客户编号、客户名称。 ok */
  154. /* 客户编号 客户名称*/
  155. select customerNo , customerName from Customer
  156. where customerNo not in(
  157. select OM.customerNo from OrderMaster OM
  158. where OM.orderNo in(
  159. select OD.orderNo from OrderDetail OD , Product P
  160. where P.productNo = OD.productNo and (P.productName = '52倍速光驱' or P.productName = '17寸显示器' )
  161. )
  162. )
  163.  
  164. /* 22 查询订单金额最高的订单编号、客户姓名、销售员姓名和相应的订单金额。ok */
  165. /* 订单编号 客户姓名 销售员姓名 订单金额 */
  166. select om.orderNo , c.customerName , e.employeeName , om.orderSum
  167. from OrderMaster om , Customer c , Employee e
  168. where c.customerNo = om.customerNo and om.employeeNo = e.employeeNo
  169. and om.orderSum >= all(
  170. select om1.orderSum from OrderMaster om1
  171. )
  172.  
  173. /* 23 查询商品“52倍速光驱”的订购数量、订购平均价和订购总金额。ok */
  174. /* 订购数量 订购平均价 订购总金额 */
  175. select sum(OD.quantity) as '订购数量', avg(OD.price) as '订购平均价', sum(OM.orderSum) as '订购总金额'
  176. from OrderMaster OM , OrderDetail OD , Product P
  177. where P.productNo = OD.productNo and OD.orderNo = OM.orderNo and P.productName = '52倍速光驱'
  178. group by P.productNo
  179.  
  180. /* 24 查询所有业务员的订单数量(给出 业务员编号,姓名,及订单数量)。ok */
  181. /* 业务员编号 姓名 订单数量*/
  182. select EM.employeeNo 业务员编号 , EM.employeeName 姓名 ,
  183. isnull((
  184. select count(*) from OrderMaster OM
  185. where OM.employeeNo = EM.employeeNo
  186. group by OM.employeeNo
  187. ),0) 订单数量
  188. from Employee EM
  189. where EM.department = '业务科'
  190.  
  191. /* 25 统计在业务科工作且在1973年出生的员工人数和平均工资。 ok */
  192. /* 员工人数 平均工资 */
  193. select count(*) as '员工人数' , avg(salary) as '平均工资' from Employee
  194. where year(birthday) = 1973 and department = '业务科'
  195.  
  196. /* 26 统计每种商品的销售数量和销售总金额。(给出商品编号及名称,并按金额的升序排序)。ok */
  197. /* 商品编号 名称 销售数量 销售总金额 */
  198. select P.productNo 商品编号 , P.productName 名称,
  199. isnull ( (
  200. select sum(OD_1.quantity) from OrderDetail OD_1
  201. where OD_1.productNo = P.productNo
  202. ) , 0) 销售数量 ,
  203. isnull ( (
  204. select sum(OD_1.quantity* OD_1.price) from OrderDetail OD_1
  205. where OD_1.productNo = P.productNo
  206. ) , 0) 销售总金额
  207. from Product P
  208. order by 销售总金额
  209.  
  210. /* 27 统计每个客户的订单数、订货总额和平均订货金额。 */
  211. select C.customerNo , C.customerName ,
  212. count(OM.orderNo) 订单数 ,
  213. isnull(sum(OM.orderSum),0) 订货总额 ,
  214. isnull(avg(OM.orderSum),0) 平均订货金额
  215. from Customer C left join OrderMaster OM on C.customerNo = OM.customerNo
  216. group by C.customerNo , C.customerName
  217.  
  218. /* 28 查询每个客户订购的 商品所属类别、同类别商品数量及对应的订货金额。
  219. (结果显示客户名称、商品所属类别、商品数量及订货金额,并按客户编号升序和按订货金额的降序排序输出) */
  220. /* 客户名称 商品所属类别 同类别商品数量 订货金额 */
  221. select C.customerName , X.productClass , X.num 同类别商品数量 , X.tot 订货金额
  222. from Customer C , (
  223. select Y.productClass , Y.customerNo , Y.num , Y.tot from (
  224. select P.productClass , OM.customerNo , sum(OD.quantity) num , sum(OD.quantity*OD.price) tot
  225. from OrderDetail OD , OrderMaster OM , Product P
  226. where OD.orderNo = OM.orderNo and P.productNo = OD.productNo
  227. group by P.productClass , OM.customerNo
  228. )Y
  229. )X
  230. where X.customerNo = C.customerNo
  231.  
  232. /* 29 查找至少有2次销售的业务员名单和销售日期。ok */
  233. /* 业务员名单 销售日期 */
  234. select EM.employeeNo , EM.employeeName , OM.orderDate
  235. from Employee EM , OrderMaster OM , (
  236. select employeeNo from OrderMaster
  237. group by employeeNo
  238. having count(employeeNo) >= 2
  239. )X
  240. where EM.employeeNo = OM.employeeNo and EM.employeeNo = X.employeeNo
  241.  
  242. /* 30 查找销售总额少于5000元的销售员编号、姓名和销售额。ok */
  243. /* 销售员编号 姓名 销售额*/
  244. select EM.employeeNo , EM.employeeName , isnull( sum(OM.orderSum) , 0 ) 销售额
  245. from Employee EM left join OrderMaster OM on EM.employeeNo = OM.employeeNo
  246. where EM.department = '业务科'
  247. group by EM.employeeNo , EM.employeeName
  248. having isnull( sum(OM.orderSum),0 )<5000
  249.  
  250. /* 31 计算每一商品每月的销售总金额。(给出商品编号、商品名称以及销售总额,并将结果首先按销售月份升序
  251. 然后按总金额降序排序输出) */
  252. /* 商品编号 商品名称 月份 销售总额 */
  253. select P.productNo ,
  254. P.productName ,
  255. isnull(month(OM.orderDate),0) 月份 ,
  256. isnull( sum(OD.price*OD.quantity) , 0 ) 销售总额
  257. from Product P left join OrderDetail OD on P.productNo = OD.productNo
  258. left join OrderMaster OM ON OM.orderNo = OD.orderNo
  259. group by P.productNo , P.productName , isnull( month(OM.orderDate) , 0)
  260. order by isnull( month(OM.orderDate) , 0) , isnull( sum(OD.price*OD.quantity) , 0 ) desc
  261.  
  262. /* 32 查询没有订购“键盘”商品的客户编号及名称(要求使用存在量词not exists)。ok */
  263. /* 客户编号 名称*/
  264. select c.customerNo , c.customerName from Customer c
  265. where not exists(
  266. select * from OrderDetail OD , OrderMaster OM , Product P
  267. where OD.productNo = P.productNo and OD.orderNo = OM.orderNo and P.productName = '键盘'
  268. and c.customerNo = OM.customerNo
  269. )
  270.  
  271. /* 33 查询没有订购过任何商品的客户编号和客户名称(要求使用存在量词not exists)。ok */
  272. /* 客户编号 客户名称 */
  273. select c0.customerNo , c0.customerName from Customer c0
  274. where not exists(
  275. select * from Customer c , OrderMaster om
  276. where om.customerNo = c.customerNo and c.customerNo = c0.customerNo
  277. )

练习并熟练掌握交互式 SQL 语言的更多相关文章

  1. 哈工大数据库系统 实验:练习并熟练掌握交互式 SQL 语言

    实验目的:基于给定的 OrderDB 数据库, 练习并熟练掌握交互式 SQL 语言实验环境:sql sever 2008 附:OrderDB 表结构及表间的关系 /* 1 查询职工工资按高低排序的前2 ...

  2. 第14讲:嵌入式SQL语言(基本技巧)

    一.交互式SQL的局限 & 嵌入式SQL的必要性 专业人员(如DBA)可以熟练地运用交互式SQL语言,但普通用户却不是那么容易上手,所以需要通过数据库应用程序来使用数据库.编写一个可以与数据库 ...

  3. SQL入门(4): 嵌入式SQL语言

    本节讲述内容: 1.嵌入式SQL 语言概述 2.变量声明与数据库连接 3.数据集与游标 4.可滚动游标与数据库的增删改 5.状态捕捉以及错误处理机制 (一)嵌入式SQL语言 之前我们所学的都是交互式S ...

  4. 数据库系统学习(九)-嵌入式SQL语言之基本技巧

    第九讲 嵌入式SQL语言之基本技巧 901 什么是嵌入式SQL语言 交互式SQL语言的局限性 嵌入式SQL语言 交互式和嵌入式语言的对比 高级语言中使用嵌入式语言需要解决的问题 902 程序与数据库连 ...

  5. SQL 语言 - 数据库系统原理

    SQL 发展历程 从 1970 年美国 IBM 研究中心的 E.F.Codd 发表论文到 1974 年 Boyce 和 Chamberlin 把 SQUARE 语言改为 SEQUEL 语言,到现在的 ...

  6. Impala SQL 语言元素(翻译)[转载]

    原 Impala SQL 语言元素(翻译) 本文来源于http://my.oschina.net/weiqingbin/blog/189413#OSC_h2_2 摘要 http://www.cloud ...

  7. Spark GraphX宝刀出鞘,图文并茂研习图计算秘笈与熟练的掌握Scala语言【大数据Spark实战高手之路】

    Spark GraphX宝刀出鞘,图文并茂研习图计算秘笈 大数据的概念与应用,正随着智能手机.平板电脑的快速流行而日渐普及,大数据中图的并行化处理一直是一个非常热门的话题.图计算正在被广泛地应用于社交 ...

  8. Impala SQL 语言元素(翻译)

    摘要: http://www.cloudera.com/content/cloudera-content/cloudera-docs/Impala/latest/Installing-and-Usin ...

  9. 已看1.熟练的使用Java语言进行面向对象程序设计,有良好的编程习惯,熟悉常用的Java API,包括集合框架、多线程(并发编程)、I/O(NIO)、Socket、JDBC、XML、反射等。[泛型]\

    1.熟练的使用Java语言进行面向对象程序设计,有良好的编程习惯,熟悉常用的Java API,包括集合框架.多线程(并发编程).I/O(NIO).Socket.JDBC.XML.反射等.[泛型]\1* ...

随机推荐

  1. VC如何得到一个文件夹的路径

    VC中没有现成的函数来选择一个文件夹,但这是经常会用到的,怎么办?自动动手,丰衣足食! 使用SHBrowseForFolder,代码如下: #include   int SelFolder(HWND ...

  2. K-means算法(理论+opencv实现)

    写在前面:之前想分类图像的时候有看过k-means算法,当时一知半解的去使用,不懂原理不懂使用规则...显然最后失败了,然后看了<机器学习>这本书对k-means算法有了理论的认识,现在通 ...

  3. 《opencv学习》 之 OTSU算法实现二值化

    主要讲解OTSU算法实现图像二值化:    1.统计灰度级图像中每个像素值的个数. 2.计算第一步个数占整个图像的比例. 3.计算每个阈值[0-255]条件下,背景和前景所包含像素值总个数和总概率(就 ...

  4. 37. CentOS-6.3安装配置Weblogic-10

    安装说明 安装环境:CentOS-6.3-x64软件:server1001_ccjk_linux32.bin安装方式:bin文件安装 安装位置:/usr/local/weblogic/下载地址:htt ...

  5. maven错误

    maven-enforcer-plugin (goal "enforce") is ignored by m2e. Plugin execution not covered by ...

  6. 机器学习入门-主成分分析(PCA)

    主成分分析: 用途:降维中最常用的一种方法 目标:提取有用的信息(基于方差的大小) 存在的问题:降维后的数据将失去原本的数据意义 向量的内积:A*B = |A|*|B|*cos(a) 如果|B| = ...

  7. 程序员教程-9章-C程序设计

    目录结构: 9.1 C语言基础 9.1.1 数据类型 1 基本数据类型 2 数组.字符数组与字符串 3 枚举类型 4 结构体.共用体和typedef 9.1.2 运算符与表达式 9.1.3 输入/输出 ...

  8. 数据报表之Excel操作模块

    Excel是当今最流行的电子表格处理软件,支持丰富的计算函数及图表,在系统运营方面广泛用于运营数据报表,比如业务质量.资源利用.安全扫描等报表,同时也是应用系统常见的文件导出格式,以便数据使用人员做进 ...

  9. RISC处理器

     RISC(精简指令集算法)处理器是经过硬件的精简只执行很有限的最常用的那部分指令的处理器.因为通过研究发现,只有 大约 20%的指令是最常用的,把处理器能执行的指令数目减少到 最低限度,对它们的执行 ...

  10. 【转载】Apache Jena TDB CRUD operations

    Apache Jena TDB CRUD operations June 11, 2015 by maltesander http://tutorial-academy.com/apache-jena ...