学习数据库必须掌握的54条SQL查询语句资料_第1页
学习数据库必须掌握的54条SQL查询语句资料_第2页
学习数据库必须掌握的54条SQL查询语句资料_第3页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、出,2345678910111213141516171819202122232425262728293031323334353637383940学习数据库必须掌握的 54 条 SQL 查询语句, 并按部门排序输1 -1 、查找员工的编号、姓名、部门和出生日期,如果出生日期为空值,显示日期不详日期格式为 yyyy-mm-dd 。select emp_no,emp_name,dept, isnull ( convert (char ( 10),birthday, 120), ' 日期不详 ' ) birthdayfrom employee order by dept-2 、查找与喻

2、自强在同一个单位的员工姓名、性别、部门和职称select emp_no,emp_name,dept,titlefrom employeewhere emp_name<>' 喻自强 ' and dept in( select dept from employeewhere emp_name=' 喻自强 ' )-3 、按部门进行汇总,统计每个部门的总工资 select dept, sum(salary)from employeegroup by dept-4 、查找商品名称为 14 寸显示器商品的销售情况,显示该商品的编号、销售数量、单价和金额 sele

3、ct d_id,qty,unit_price,unit_price *qty totpricefrom sale_item a,product bwhere d_id =d_id and prod_name ='14 寸显示器 '-5 、在销售明细表中按产品编号进行汇总,统计每种产品的销售数量和金额 select prod_id, sum(qty) totqty, sum(qty * unit_price) totprice from sale_item group by prod_id-6 、使用 convert 函数按客户编号统计每个客户 19

4、96 年的订单总金额 select cust_id, sum(tot_amt) totpricefrom saleswhere convert (char ( 4),order_date, 120) ='1996' group by cust_id-7 、查找有销售记录的客户编号、名称和订单总额 select a.cust_id,cust_name, sum(tot_amt) totprice from customer a,sales b where a.cust_id =b.cust_id group by a.cust_id,cust_name-8 、查找在 1997 年

5、中有销售记录的客户编号、名称和订单总额41 select a.cust_id,cust_name, sum(tot_amt) totprice42 from customer a,sales b43 where a.cust_id =b.cust_id and convert (char ( 4),order_date, 120) ='1997'44 group by a.cust_id,cust_name4546 -9 、查找一次销售最大的销售记录47 select order_no,cust_id,sale_id,tot_amt48 from sales49 where t

6、ot_amt =50 ( select max(tot_amt)51 from sales)5253 -10 、查找至少有 3 次销售的业务员名单和销售日期54 select emp_name,order_date55 from employee a,sales b56 where emp_no=sale_id and a.emp_no in57 ( select sale_id58 from sales59 group by sale_id60 having count (*) >=3)61 order by emp_name6263 -11 、用存在量词查找没有订货记录的客户名称64

7、 select cust_name65 from customer a66 where not exists67 ( select *68 from sales b69 where a.cust_id =b.cust_id)7071 -12 、使用左外连接查找每个客户的客户编号、名称、订货日期、订单金额订货日期不要显示时间, 日期格式为 yyyy-mm-dd 按客户编号排序,同一客户再按订单降序排序输出72 select a.cust_id,cust_name, convert (char ( 10),order_date, 120),tot_amt73 from customer a lef

8、t outer join sales b on a.cust_id =b.cust_id74 order by a.cust_id,tot_amt desc7576 -13 、查找 16M DRAM的销售情况,要求显示相应的销售员的姓名、性别,销售日期、销售数量和金额, 其中性别用男、女表示77 select emp_name 姓名 , 性别 = case a.sex when 'm' then ' 男 '78 when 'f' then ' 女 '79 else ' 未 '80 end,81 销售日期 = isn

9、ull ( convert ( char ( 10),c.order_date, 120), ' 日期不详 ' ),82 qty 数量 , qty * unit_price as 金额83 from employee a, sales b, sale_item c,product d84 where d_name ='16M DRAM' and d_id =d_id and85 a.emp_no =b.sale_id and b.order_no =c.order_no8687 -14 、查找每个人的销售记录,要求显示销售员的编号、

10、姓名、性别、产品名称、 和销售日期88 select emp_no 编号 ,emp_name 姓名 , 性别 = case a.sex when 'm' then89 when 'f' then ' 女 '90 else ' 未 '91 end,92 prod_name 产品名称 , 销售日期 = isnull ( convert ( char ( 10),c.order_date,93 qty 数量 , qty * unit_price as 金额94 from employee a left outer join sales

11、b on a.emp_no =b.sale_id , sale95 where d_id =d_id and b.order_no =c.order_no9697 -15 、查找销售金额最大的客户名称和总货款98 select cust_name,d.cust_sum99 from customer a,100 ( select cust_id,cust_sum101 from ( select cust_id, sum(tot_amt) as cust_sum102 from sales103 group by cust_id ) b104 where b.cust_su

12、m =105 ( select max(cust_sum)106 from ( select cust_id, sum(tot_amt) as cust_sum107 from sales108 group by cust_id ) c )109 ) d110 where a.cust_id =d.cust_id111112 -16 、查找销售总额少于 1000 元的销售员编号、姓名和销售额113 select emp_no,emp_name,d.sale_sum114 from employee a,115 ( select sale_id,sale_sum116 from ( select

13、 sale_id, sum(tot_amt) as sale_sum117 from sales118 group by sale_id ) b119 where b.sale_sum <1000120 ) d121 where a.emp_no =d.sale_id122数量、单价、金额'男'120), ' 日期不详 ' ),item c,product d数量和金额123 -17 、查找至少销售了 3 种商品的客户编号、客户名称、商品编号、商品名称、1241251261271281291301311321331341351361371381391401

14、41142143144145146147148149150151152153154155156157158159160161162163164165166select a.cust_id,cust_name,d_id,prod_name,d.qty,d.qty* d.unit_pricefrom customer a, product b, sales c, sale_item dwhere a.cust_id =c.cust_id and d_id =d_id andc.order_no =d.order_no and a.cust_id in ( select

15、 cust_idfrom ( select cust_id, count ( distinct prod_id) prodidfrom ( select cust_id,prod_idfrom sales e,sale_item fwhere e.order_no =f.order_no) ggroup by cust_idhaving count ( distinct prod_id) >=3) h )-18 、查找至少与世界技术开发公司销售相同的客户编号、名称和商品编号、商品名称、数量和金额 select a.cust_id,cust_name,d_id,prod_name

16、,qty,qty* unit_pricefrom customer a, product b, sales c, sale_item dwhere a.cust_id =c.cust_id and d_id =d_id andc.order_no =d.order_no and not exists( select f. *from customer x ,sales e, sale_item fwhere cust_name =' 世界技术开发公司 ' and x.cust_id =e.cust_id ande.order_no =f.order_no a

17、nd not exists( select g. *from sale_item g, sales hwhere d_id = d_id and g.order_no =h.order_no andh.cust_id =a.cust_id)19、查找表中所有姓刘的职工的工号,部门,薪水select emp_no,emp_name,dept,salaryfrom employeewhere emp_name like ' 刘 %'20、查找所有定单金额高于 2000 的所有客户编号 select cust_id from saleswhere tot_amt

18、>200021、统计表中员工的薪水在 4000- 6000 之间的人数 select count (*) as 人数from employeewhere salary between 4000 and 600022、查询表中的同一部门的职工的平均工资,但只查询住址是上海市的员工select avg(salary) avg_sal,dept from employee where addr like ' 上海市 %' group by dept167168169170171172173174175176177178179180181182183184185186187188

19、18919019119219319419519619719819920020120220320420520620720820923、将表中住址为 " 上海市 " 的员工住址改为 " 北京市 " update employee set addr like ' 北京市 ' where addr like ' 上海市 '24、查找业务部或会计部的女员工的基本信息。select emp_no,emp_name,deptfrom employeewhere sex ='F' and dept in ( ' 业

20、务 ' , ' 会计 ' )25、显示每种产品的销售金额总和,并依销售金额由大到小输出。 select prod_id , sum(qty * unit_price)from sale_itemgroup by prod_idorder by sum(qty * unit_price) desc26、选取编号界于 'C0001' 和'C0004' 的客户编号、客户名称、客户地址 select CUST_ID,cust_name,addrfrom customerwhere cust_id between 'C0001' A

21、ND 'C0004'27、计算出一共销售了几种产品。select count ( distinct prod_id) as ' 共销售产品数 ' from sale_item28、将业务部员工的薪水上调 3%。 update employeeset salary =salary *1.03where dept =' 业务 '29、由 employee 表中查找出薪水最低的员工信息。 select *from employeewhere salary =( select min(salary )from employee )30、使用 join 查

22、询客户姓名为 "客户丙"所购货物的 "客户名称"," 定单金额"," 定货日期"," 电话号码"210 select a.cust_id,b.tot_amt,b.order_date,a.tel_no211 from customer a join sales b212 on a.cust_id =b.cust_id and cust_name like ' 客户丙 '213" 的所214 31、由 sales 表中查找出订单金额大于 "E0013 业务员在

23、1996/ 10/15 这天所接每一张订单的金额 有订单。215 select *216 from sales217 where tot_amt >all218 ( select tot_amt219 from sales220 where sale_id ='E0013' and order_date ='1996/10/15' )221 order by tot_amt222223 32、计算 'P0001' 产品的平均销售单价224 select avg(unit_price)225 from sale_item226 where p

24、rod_id ='P0001'227228 33、找出公司女员工所接的定单229 select sale_id,tot_amt230 from sales231 where sale_id in232 ( select sale_id from employee233 where sex ='F' )234235 34、找出同一天进入公司服务的员工236 select a.emp_no,a.emp_name,a.date_hired237 from employee a238 join employee b239 on (a.emp_no != b.emp_no

25、 and a.date_hired =b.date_hired)240 order by a.date_hired241242 35、找出目前业绩超过 232000 元的员工编号和姓名。243 select emp_no,emp_name244 from employee245 where emp_no in246 ( select sale_id247 from sales248 group by sale_id249 having sum(tot_amt) <232000)250 251 36、查询出 employee 表中所有女职工的平均工资和住址在上海市的所有女职工的平均工资25

26、2253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294select avg(salary)from employeewhere sex like 'f'unionselect avg(salary)from employeewhere sex like 'f' and addr like ' 上海市 %'37、在 employee 表中查询薪水超过员

27、工平均薪水的员工信息。Select *from employeewhere salary >( select avg (salary)from employee)38、找出目前销售业绩超过 10000 元的业务员编号及销售业绩,并按销售业绩从大到小排序。 Select sale_id , sum(tot_amt)from salesgroup by sale_idhaving sum(tot_amt) >10000order by sum(tot_amt) desc39、找出公司男业务员所接且订单金额超过2000 元的订单号及订单金额。Select order_no,tot_amt

28、From sales ,employeeWhere sale_id =emp_no and sex ='M' and tot_amt >200040、查询 sales 表中订单金额最高的订单号及订单金额。Select order_no,tot_amt from sales where tot_amt =( select max(tot_amt) from sales)41、查询在每张订单中订购金额超过 4000 元的客户名及其地址。Select cust_name,addr from customer a,sales bwhere a.cust_id =b.cust_id

29、 and tot_amt >400042、求出每位客户的总订购金额,显示出客户号及总订购金额,并按总订购金额降序排列Select cust_id, sum(tot_amt) from salesGroup by cust_idOrder by sum(tot_amt) desc43、求每位客户订购的每种产品的总数量及平均单价,并按客户号,产品号从小到大排列 Select cust_id,prod_id, sum(qty), sum(qty * unit_price) / sum(qty)From sales a, sale_item bWhere a.order_no =b.order_

30、no295 Group by cust_id,prod_id296 Order by cust_id,prod_id297298 44、 查询订购了三种以上产品的订单号。299 Select order_no300 from sale_item301 Group by order_no302 Having count (*) >3303304 45、 查询订购的产品至少包含了订单 3 号中所订购产品的订单。305 Select distinct order_no306 From sale_item a307 Where order_no <>'3' and n

31、ot exists (308 Select * from sale_item b where order_no ='3' and not exists309 ( select * from sale_item c where c.order_no =a.order_no and d_id =d_id)310311 46、 在 sales 表中查找出订单金额大于 "E0013 业务员在 1996/ 11/ 10 这天所接每一张订单的金额 "的 所有订单,并显示承接这些订单的业务员和该订单的金额。312 Select sale_id,tot_

32、amt from sales313 where tot_amt >all ( select tot_amt314 from sales315 where sale_id ='E0013' and order_date ='1996-11-10' )316317 47、 查询末承接业务的员工的信息。318 Select *319 From employee a320 Where not exists321 ( select * from sales b where a.emp_no =b.sale_id)322323 48、 查询来自上海市的客户的姓名,电话

33、、订单号及订单金额。324 Select cust_name,tel_no,order_no,tot_amt325 From customer a ,sales b326 Where a.cust_id =b.cust_id and addr =' 上海市 '327328 49、 查询每位业务员各个月的业绩,并按业务员编号、月份降序排序。329 Select sale_id, month(order_date), sum(tot_amt)330 from sales331 group by sale_id, month(order_date)332 order by sale_id, month(order_date) desc333334 50、 求每种产品的总销售数量及总销售金额,要求显示出产品编号、产品名称,总数量及总金额,并 按产品号从小到大排列。335 Selec

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论