




已阅读5页,还剩10页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
11级信管,保密,图档上机考试题目与参考答案3.3Simple Select Statements1. EXAMPLE 3.3.1find the aid values and names of agents that are based in New York. select aid, aname from agents where city=New York;2. EXAMPLE 3.3.3Retrieve all pid values of parts for which orders are placed.select distinct pid from orders;3. EXAMPLE 3.3.4retrieve all customer-agent name pairs, (cname, aname), where the customer places an order through the agent. select distinct ame,agents.anamefrom customers,orders,agentswhere customers.cid=orders.cid and orders.aid=agents.aid;4. EXAMPLE 3.3.6all pairs of customers based in the same city. select c1.cid, c2.cidfrom customers c1, customers c2where c1.city = c2.city and c1.cid c2.cid;5. EXAMPLE 3.3.7find pid values of products that have been ordered by at least two customers. select distinct x1.pidfrom orders x1, orders x2where x1.pid = x2.pid and x1.cid x2.cid;6. EXAMPLE 3.3.8Get cid values of customers who order a product for which an order is also placed by agent a06. select distinct y.cidfrom orders x, orders ywhere y.pid = x,pid and x.aid = a06;3.4Subqueries7. EXAMPLE 3.4.1Get cid values of customers who place orders with agents in Duluth or Dallas. select distinct cid from orderswhere aid in (select aid from agentswhere city= Duluth or city = Dallas)8. EXAMPLE 3.4.2to retrieve all information concerning agents based in Duluth or Dallas (very close to the Subquery in the previous example). select * from agentswhere city in (Duluth, Dallas );or select *from agents where city = Duluth or city = Dallas;9. EXAMPLE 3.4.3to determine the names and discounts of all customers who place orders through agents in Duluth or Dallas. select distinct cname, discnt from customerswhere cid in (select cid from orders where aid in (select aid from agents where city in (Duluth, Dallas );10. EXAMPLE 3.4.4to find the names of customers who order product p05. select distinct cname from customers, orders where customers.cid = orders.cid and orders.pid = p05or select distinct cname from customers where p05 in (select pid from orders where cid = customers.cid);11. EXAMPLE 3.4.5Get the names of customers who order product p07 from agent a03.select distinct cname from customerswhere cid in (select cid from orders where pid = p07 and aid = a03)12. EXAMPLE 3.4.6to retrieve ordno values for all orders placed by customers in Duluth through agents in New York. select ordno from orders x where exists (select * from customers c, agents a where c.cid = x.cid and a.aid = x.aid and c.city = Duluth and a.city=New York);13. EXAMPLE 3.4.7find aid values of agents with a minimum percent commission. select aid from agents where percent = (select min(percent) from agents);14. EXAMPLE 3.4.8find all customers who have the same discount as that of any of the customers in Dallas or Boston. select cid, cname from customerswhere discnt = some (select discnt from customers where city = Dallas or city = Boston); 15. EXAMPLE 3.4.9Get cid values of customers with discnt smaller than those of any customers who live in Duluth. select cid from customers where discnt all (select discnt from customers where city = Duluth);16. EXAMPLE 3.4.10Retrieve all customer names where the customer places an order through agent a05. select distinct ame from customers c where exists (select * from orders x where c.cid = x.cid and x.aid = a05);or select distinct ame from customers c, orders x where c.cid = x.cid and x.aid = a05 ;17. EXAMPLE 3.4.11Get cid values of customers who order both products p01 and p07.select distinct cid from orders xwhere pid = p01 and exsits (select * from orders where cid = x.cid and pid = p07);or select distinct x.cid from orders x, orders y where x.pid = p01 and x.cid = y.cid and y.pid = p07;18. EXAMPLE 3.4.12Retrieve all customer names where the customer does not place an order through agent a05.select distinct ame from customers cwhere not exists (select * from orders x where c.cid = x.cid and x.aid = a05);19. EXAMPLE 3.4.13retrieving all customer names where the customer does not place an order through agent a05, but using the two equivalent NOT IN and ALL predicates in place of NOT EXISTS. select distinct ame from customers c where c.cid not in (select cid from orders where aid = a05);or select ame from customers c where c.cid all (select cid from orders where aid = a05);20. EXAMPLE 3.4.14Find cid values of customers who do not place any order through agent a03.select distinct cid from orders x where not exists (select * from orderswhere cid = x.cid and aid = a03);or select cid from customers c where not exists (select * from orderswhere cid = c.cid and aid = a03);21. EXAMPLE 3.4.15Retrieve the city names containing customers who order product p01.select distinct city from customers where cid in (select cid from orders where pid = p01);or select distinct city from customers where cid =some (select cid from orders where pid = p01);or select distinct city from customers c where exsits (select * from orders where cid = c.cid and pid = p01);or select distinct city from customers c, orders x where x.cid = c.cid and x.pid = p01;or select distinct city from customers c where p01 in (select pid from orders where cid = c.cid);3.5UNION Operators and FOR ALL Conditions22. EXAMPLE 3.5.1to create a list of cities where either a customer or an agent, or both, is based.select city from customers union select city from agents;23. EXAMPLE 3.5.2Get the cid values of customers who place orders with all agents based in New York.select c.cid from customers c where not exsits (select * from agents awhere a.city = New York and not exsits (select * from orders xwhere x.cid = c.cid and x.aid = a.aid);24. EXAMPLE 3.5.3Get the aid values of agents in New York or Duluth who place orders for all products costing more than a dollar.select aid from agents a where (a.city = New York or a.city = Duluth) and not exsits (select p.pid from products pwhere p.price 1.00 and not exsits (select * from orders xwhere x.pid = p.pid and x.aid = a.aid);25. EXAMPLE 3.5.4Find aid values of agents who place orders for product p01 as well as for all products costing more than a dollar. select a.aid from agents a where a.aid in (select aid from orders where pid = p01) and not exsits (select p.pid from products p where p.price 1.00 and not exsits (select * from orders xwhere x.pid = p.pid and x.aid = a.aid);or select distinct y.aid from orders y where y.pid = p01 and not exsits (select p.pid from products p where p.price 1.00 and not exsits (select * from orders x where x.pid = p.pid and x.aid = y.aid);26. EXAMPLE 3.5.6Find pid values of products supplied to all customers in Duluth. select pid from products pwhere not exsits (select c.cid from customers cwhere c.city = Duluthand not exists(select * from orders xwhere x.pid = p.pid and x.cid = c.cid);3.7 Set Functions in SQL27. EXAMPLE 3.7.1determine the total dollar amount of all orders. select sum(dollars) as totaldollars from orders28. EXAMPLE 3.7.2To determine the total quantity of product p03 that has been ordered.select sum(qty) as TOTAL from orders where pid=p0329. EXAMPLE 3.7.4Get the number of cities where customers are based. select count(distinct city) from customers30. EXAMPLE 3.7.5List the cid values of alt customers who have a discount less than the maximum discount. select cid from customerswhere discnt (select max(discnt) from customers)31. EXAMPLE 3.7.6Find products ordered by at least two customers. select p.pid from products pwhere 2 100037. EXAMPLE 3.8.4Provide pid values of all products purchased by at least two customers.select distinct pid from ordersgroup by pidhaving count(distinct cid) = 23.9 A Complete Description of SQL Select38. EXAMPLE 3.9.1List all customers, agents, and the dollar sales for pairs of customers and agents, and order the result from largest to smallest sales totals. Retain only those pairs for which the dollar amount is at least equal to 900.00.select ame, c.cid, a.aname, a.aid, sum(dollars) as casalesfrom customers c, orders o, agents awhere c.cid = o.cid, and a.aid = o.aidgroup by ame, c.cid, a.aname, a.aidhaving sum(o.dollars) = 900.00order by casales desc39. EXAMPLE 3.9.2listed the cid values of all customers with a discount less than the maximum discount. select cid from customerswhere discnt = all (select discnt from customers dwhere d.cidc.cid)41. EXAMPLE 3.9.4Retrieve all data about customers whose cname begins with the letter “A”. select * from customers where cname like A%42. EXAMPLE 3.9.5Retrieve cid values of customers whose cname does not have a third letter equal to “%”. select cid from customers where cname not like _%43. EXAMPLE 3.9.6Retrieve cid values of customers whose cname begins “Tip_” and has an arbitrary number of characters following.select cid from customers where cname like TIP_%44. EXAMPLE 3.9.7Retrieve cid values of customers whose cname starts with the sequence “ab”. select cid from customers where cname like ab%3.10 Insert, Update, and Delete Statements45. EXAMPLE 3.10.1Add a row with specified values to the orders table, setting the qty and dollars columns null. insert into orders (ordno, month, cid, aid, pid)values (1107, aug, c006, a04, p01)46. EXAMPLE 3.10.2Create a new table called swcusts of Southwestern customers, and insert
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 部门级安全培训材料课件
- 【高考模拟】2026届普通高等学校招生全国统一考试最 新 数学模拟试卷3(含解析)
- 海藻酸盐包装力学优化-洞察及研究
- 部署消防安全培训课件
- 遨游汉字课件
- 2023-2024学年广州市东江外语实验学校八年级(下)第一次限时训练物理试题
- 2025年广东省广州市中考物理三轮冲刺《机械运动》
- 国际标准体系差异对出口型刨刀产品认证成本的结构性挤压效应
- 口腔解剖变异区域刮治效率提升的3D打印定制化工具设计
- 反向刮水器总成在极端环境(-40℃至85℃)下的热应力变形补偿机制
- 设备部工作清单
- JJG 291-2018溶解氧测定仪
- 蒙克《呐喊》赏析
- 绝经前后诸证(中医妇科学)
- 甲状腺癌健康宣传知识课件
- 铁路交通事故调查处理-铁路交通事故
- 花海的设计方案
- 作文提纲课件
- 静设备安装质量控制过程
- 个人借款协议书范文:免修版模板范本
- 孙燕姿所有歌曲歌词大全(11张专辑)
评论
0/150
提交评论