仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 662|回复: 8
打印 上一主题 下一主题

[学习教程] MSSQL网页设计sql进修

[复制链接]
灵魂腐蚀 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:38:53 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
两个到底是哪一个给出了MySQL这个名字至今依然是个迷,包括开发者在内也不知道。sql进修123456789101112............ABCDDEF 
 -1、查找员工的编号、姓名、部门和出身日期,假如出身日期为空值,
--显现日期,并按部门排序输入,日期格局为yyyy-mm-dd。
selectemp_no,emp_name,dept,
isnull(convert(char(10),birthday,120),日期)birthday
fromemployee
orderbydept
--2、查找与喻自强在统一个单元的员工姓名、性别、部门和职称
selectemp_no,emp_name,dept,title
fromemployee
whereemp_name喻自强anddeptin
(selectdeptfromemployee
whereemp_name=喻自强)

--3、按部门举行汇总,统计每一个部门的总人为
selectdept,sum(salary)
fromemployee
groupbydept

--4、查找商品称号为14寸显现器商品的发卖情形,
--显现该商品的编号、发卖数目、单价和金额
selecta.prod_id,qty,unit_price,unit_price*qtytotprice
fromsale_itema,productb
wherea.prod_id=b.prod_idandprod_name=14寸显现器

--5、在发卖明细表中按产物编号举行汇总,统计每种产物的发卖数目和金额
selectprod_id,sum(qty)totqty,sum(qty*unit_price)totprice
fromsale_item
groupbyprod_id

--6、利用convert函数按客户编号统计每一个客户1996年的定单总金额
selectcust_id,sum(tot_amt)totprice
fromsales
whereconvert(char(4),order_date,120)=1996
groupbycust_id

--7、查找有发卖纪录的客户编号、称号和定单总额
selecta.cust_id,cust_name,sum(tot_amt)totprice
fromcustomera,salesb
wherea.cust_id=b.cust_id
groupbya.cust_id,cust_name

--8、查找在1997年中有发卖纪录的客户编号、称号和定单总额
selecta.cust_id,cust_name,sum(tot_amt)totprice
fromcustomera,salesb
wherea.cust_id=b.cust_idandconvert(char(4),order_date,120)=1997
groupbya.cust_id,cust_name

--9、查找一次发卖最年夜的发卖纪录
selectorder_no,cust_id,sale_id,tot_amt
fromsales
wheretot_amt=
(selectmax(tot_amt)
fromsales)

--10、查找最少有3次发卖的营业员名单和发卖日期
selectemp_name,order_date
fromemployeea,salesb
whereemp_no=sale_idanda.emp_noin
(selectsale_id
fromsales
groupbysale_id
havingcount(*)>=3)
orderbyemp_name

--11、用存在量词查找没有定货纪录的客户称号
selectcust_name
fromcustomera
wherenotexists
(select*
fromsalesb
wherea.cust_id=b.cust_id)

--12、利用左外毗连查找每一个客户的客户编号、称号、定货日期、定单金额
--定货日期不要显现工夫,日期格局为yyyy-mm-dd
--按客户编号排序,统一客户再按定单降序排序输入
selecta.cust_id,cust_name,convert(char(10),order_date,120),tot_amt
fromcustomeraleftouterjoinsalesbona.cust_id=b.cust_id
orderbya.cust_id,tot_amtdesc

--13、查找16MDRAM的发卖情形,请求显现响应的发卖员的姓名、
--性别,发卖日期、发卖数目和金额,个中性别用男、女暗示
selectemp_name姓名,性别=casea.sexwhenmthen男
whenfthen女
else未
end,
发卖日期=isnull(convert(char(10),c.order_date,120),日期),
qty数目,qty*unit_priceas金额
fromemployeea,salesb,sale_itemc,productd
whered.prod_name=16MDRAMandd.prod_id=c.prod_idand
a.emp_no=b.sale_idandb.order_no=c.order_no

--14、查找每一个人的发卖纪录,请求显现发卖员的编号、姓名、性别、
--产物称号、数目、单价、金额和发卖日期
selectemp_no编号,emp_name姓名,性别=casea.sexwhenmthen男
whenfthen女
else未
end,
prod_name产物称号,发卖日期=isnull(convert(char(10),c.order_date,120),日期),
qty数目,qty*unit_priceas金额
fromemployeealeftouterjoinsalesbona.emp_no=b.sale_id,sale_itemc,productd
whered.prod_id=c.prod_idandb.order_no=c.order_no

--15、查找发卖金额最年夜的客户称号和总货款
selectcust_name,d.cust_sum
fromcustomera,
(selectcust_id,cust_sum
from(selectcust_id,sum(tot_amt)ascust_sum
fromsales
groupbycust_id)b
whereb.cust_sum=
(selectmax(cust_sum)
from(selectcust_id,sum(tot_amt)ascust_sum
fromsales
groupbycust_id)c)
)d
wherea.cust_id=d.cust_id

--16、查找发卖总额少于1000元的发卖员编号、姓名和发卖额
selectemp_no,emp_name,d.sale_sum
fromemployeea,
(selectsale_id,sale_sum
from(selectsale_id,sum(tot_amt)assale_sum
fromsales
groupbysale_id)b
whereb.sale_sum<1000
)d
wherea.emp_no=d.sale_id

--17、查找最少发卖了3种商品的客户编号、客户称号、商品编号、商品称号、数目和金额
selecta.cust_id,cust_name,b.prod_id,prod_name,d.qty,d.qty*d.unit_price
fromcustomera,productb,salesc,sale_itemd
wherea.cust_id=c.cust_idandd.prod_id=b.prod_idand
c.order_no=d.order_noanda.cust_idin(
selectcust_id
from(selectcust_id,count(distinctprod_id)prodid
from(selectcust_id,prod_id
fromsalese,sale_itemf
wheree.order_no=f.order_no)g
groupbycust_id
havingcount(distinctprod_id)>=3)h)

--18、查找最少与天下手艺开辟公司发卖不异的客户编号、称号和商品编号、商品称号、数目和金额
selecta.cust_id,cust_name,d.prod_id,prod_name,qty,qty*unit_price
fromcustomera,productb,salesc,sale_itemd
wherea.cust_id=c.cust_idandd.prod_id=b.prod_idand
c.order_no=d.order_noandnotexists
(selectf.*
fromcustomerx,salese,sale_itemf
wherecust_name=天下手艺开辟公司andx.cust_id=e.cust_idand
e.order_no=f.order_noandnotexists
(selectg.*
fromsale_itemg,salesh
whereg.prod_id=f.prod_idandg.order_no=h.order_noand
h.cust_id=a.cust_id)
)

19、查找表中一切姓刘的职工的工号,部门,薪水
selectemp_no,emp_name,dept,salary
fromemployee
whereemp_namelike刘%

20、查找一切订单金额高于2000的一切客户编号
selectcust_id
fromsales
wheretot_amt>2000

21、统计表中员工的薪水在4000-6000之间的人数
selectcount(*)as人数
fromemployee
wheresalarybetween4000and6000

22、查询表中的统一部门的职工的均匀人为,但只查询"住址"是"上海市"的员工
selectavg(salary)avg_sal,dept
fromemployee
whereaddrlike上海市%
groupbydept

23、将表中住址为"上海市"的员工住址改成"北京市"
updateemployee
setaddrlike北京市
whereaddrlike上海市

24、查找营业部或管帐部的女员工的基础信息。
selectemp_no,emp_name,dept
fromemployee
wheresex=Fanddeptin(营业,管帐)

25、显现每种产物的发卖金额总和,并依发卖金额由年夜到小输入。
selectprod_id,sum(qty*unit_price)
fromsale_item
groupbyprod_id
orderbysum(qty*unit_price)desc

26、拔取编号界于C0001和C0004的客户编号、客户称号、客户地点。
selectCUST_ID,cust_name,addr
fromcustomer
wherecust_idbetweenC0001ANDC0004

27、盘算出一共发卖了几种产物。
selectcount(distinctprod_id)as共发卖产物数
fromsale_item

28、将营业部员工的薪水上调3%。
updateemployee
setsalary=salary*1.03
wheredept=营业

29、由employee表中查找出薪水最低的员工信息。
select*
fromemployee
wheresalary=
(selectmin(salary)
fromemployee)

30、利用join查询客户姓名为"客户丙"所购货品的"客户称号","订单金额","订货日期","德律风号码"
selecta.cust_id,b.tot_amt,b.order_date,a.tel_no
fromcustomerajoinsalesb
ona.cust_id=b.cust_idandcust_namelike客户丙

31、由sales表中查找出定单金额年夜于"E0013营业员在1996/10/15是日所接每张定单的金额"的一切定单。
select*
fromsales
wheretot_amt>all
(selecttot_amt
fromsales
wheresale_id=E0013andorder_date=1996/10/15)
orderbytot_amt

32、盘算P0001产物的均匀发卖单价
selectavg(unit_price)
fromsale_item
whereprod_id=P0001

33、找出公司女员工所接的订单
selectsale_id,tot_amt
fromsales
wheresale_idin
(selectsale_idfromemployee
wheresex=F)

34、找出统一天进进公司服务的员工
selecta.emp_no,a.emp_name,a.date_hired
fromemployeea
joinemployeeb
on(a.emp_no!=b.emp_noanda.date_hired=b.date_hired)
orderbya.date_hired

35、找出今朝事迹凌驾232000元的员工编号和姓名。
selectemp_no,emp_name
fromemployee
whereemp_noin
(selectsale_id
fromsales
groupbysale_id
havingsum(tot_amt)<232000)

36、查询出employee表中一切女职工的均匀人为和住址在"上海市"的一切女职工的均匀人为
selectavg(salary)
fromemployee
wheresexlikef
union
selectavg(salary)
fromemployee
wheresexlikefandaddrlike上海市%

37、在employee表中查询薪水凌驾员工均匀薪水的员工信息。
Select*
fromemployee
wheresalary>(selectavg(salary)
fromemployee)

38、找出今朝发卖事迹凌驾10000元的营业员编号及发卖事迹,并按发卖事迹从年夜到小排序。
Selectsale_id,sum(tot_amt)
fromsales
groupbysale_id
havingsum(tot_amt)>10000
orderbysum(tot_amt)desc

39、找出公司男营业员所接且定单金额凌驾2000元的定单号及定单金额。
Selectorder_no,tot_amt
Fromsales,employee
Wheresale_id=emp_noandsex=Mandtot_amt>2000

40、查询sales表中定单金额最高的定单号及定单金额。
Selectorder_no,tot_amtfromsales
wheretot_amt=(selectmax(tot_amt)fromsales)

41、查询在每张定单中订购金额凌驾4000元的客户名及其地点。
Selectcust_name,addrfromcustomera,salesb
wherea.cust_id=b.cust_idandtot_amt>4000

42、求出每位客户的总订购金额,显现出客户号及总订购金额,并按总订购金额降序分列。
Selectcust_id,sum(tot_amt)fromsales
Groupbycust_id
Orderbysum(tot_amt)desc

43、求每位客户订购的每种产物的总数目及均匀单价,并按客户号,产物号从小到年夜分列。
Selectcust_id,prod_id,sum(qty),sum(qty*unit_price)/sum(qty)
Fromsalesa,sale_itemb
Wherea.order_no=b.order_no
Groupbycust_id,prod_id
Orderbycust_id,prod_id

44、查询订购了三种以上产物的定单号。
Selectorder_no
fromsale_item
Groupbyorder_no
Havingcount(*)>3

45、查询订购的产物最少包括了定单3号中所订购产物的定单。
Selectdistinctorder_no
Fromsale_itema
Whereorder_no3andnotexists(
Select*fromsale_itembwhereorder_no=3andnotexists
(select*fromsale_itemcwherec.order_no=a.order_noandc.prod_id=b.prod_id))

46、在sales表中查找出定单金额年夜于"E0013营业员在1996/11/10是日所接每张定单的金额"的一切定单,并显现承接这些定单的营业员和该定单的金额。
Selectsale_id,tot_amtfromsales
wheretot_amt>all(selecttot_amt
fromsales
wheresale_id=E0013andorder_date=1996-11-10)

47、查询末承接营业的员工的信息。
Select*
Fromemployeea
Wherenotexists
(select*fromsalesbwherea.emp_no=b.sale_id)

48、查询来自上海市的客户的姓名,德律风、定单号及定单金额。
Selectcust_name,tel_no,order_no,tot_amt
Fromcustomera,salesb
Wherea.cust_id=b.cust_idandaddr=上海市

49、查询每位营业员各个月的事迹,并按营业员编号、月份降序排序。
Selectsale_id,month(order_date),sum(tot_amt)
fromsales
groupbysale_id,month(order_date)
orderbysale_id,month(order_date)desc

50、求每种产物的总发卖数目及总发卖金额,请求显现生产品编号、产物称号,总数目及总金额,并按产物号从小到年夜分列。
Selecta.prod_id,prod_name,sum(qty),sum(qty*unit_price)
Fromsale_itema,productb
Wherea.prod_id=b.prod_id
Groupbya.prod_id,prod_name
Orderbya.prod_id
51、查询总订购金额凌驾C0002客户的总订购金额的客户号,客户名及其住址。
Selectcust_id,cust_name,addr
Fromcustomer
Wherecust_idin(selectcust_idfromsales
Groupbycust_id
Havingsum(tot_amt)>
(Selectsum(tot_amt)fromsaleswherecust_id=C0002))

52、查询事迹最好的的营业员号、营业员名及其总发卖金额。
selectemp_no,emp_name,sum(tot_amt)
fromemployeea,salesb
wherea.emp_no=b.sale_id
groupbyemp_no,emp_name
havingsum(tot_amt)=
(selectmax(totamt)
from(selectsale_id,sum(tot_amt)totamt
fromsales
groupbysale_id)c)

53、查询每位客户所订购的每种产物的具体清单,请求显现出客户号,客户名,产物号,产物名,数目及单价。
selecta.cust_id,cust_name,c.prod_id,prod_name,qty,unit_price
fromcustomera,salesb,sale_itemc,productd
wherea.cust_id=b.cust_idandb.order_no=c.order_noandc.prod_id=d.prod_id

54、求各部门的均匀薪水,请求按均匀薪水从小到年夜排序。
selectdept,avg(salary)
fromemployee
groupbydept
orderbyavg(salary)








优化的SQL查询算法,有效地提高查询速度
谁可相欹 该用户已被删除
沙发
发表于 2015-1-19 20:23:47 | 只看该作者
但是随着数据量的增大,这种成本差距会逐渐减小,趋于相等。(500万数量级只相差10%左右)
不帅 该用户已被删除
板凳
发表于 2015-1-25 18:03:52 | 只看该作者
在select语句中可以使用groupby子句将行划分成较小的组,然后,使用聚组函数返回每一个组的汇总信息,另外,可以使用having子句限制返回的结果集。
变相怪杰 该用户已被删除
地板
发表于 2015-2-3 12:30:16 | 只看该作者
从项目平台的选择上讲,我们关心的,应该是一款产品能不能满足任务需求,而不是网上怎么说。
愤怒的大鸟 该用户已被删除
5#
发表于 2015-2-8 23:47:06 | 只看该作者
SQL语言是学习所有数据库产品的基础,无论你是做数据库管理还是做数据库开发都是这样。不过具体学习的侧重点要看你将来做哪一块,如果是做数据库管理(DBA),侧重点应该放在SQLServer的系统管理上.
只想知道 该用户已被删除
6#
发表于 2015-2-26 13:53:07 | 只看该作者
大家注意一点。如下面的例子:
柔情似水 该用户已被删除
7#
发表于 2015-3-8 15:26:28 | 只看该作者
一个百万级别的基本信息表A,一个百万级别的详细记录表B,A中有个身份证id,B中也有身份id;先要找出A中在B的详细记录。
精灵巫婆 该用户已被删除
8#
发表于 2015-3-16 03:24:21 | 只看该作者
一直以来个人感觉SQLServer的优化器要比Oracle的聪明。SQL2005的更是比2k聪明了不少。(有次作试验发现有的语句在200万级时还比50万级的相同语句要快show_text的一些提示没有找到解释。一直在奇怪。)
活着的死人 该用户已被删除
9#
发表于 2015-3-22 19:23:03 | 只看该作者
微软对CLR作了大篇幅的宣传,这是因为数据库产品终于融入.net体系中。最开始我们也是狂喜,感觉对象数据库的一些概念可以实现了。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-6-8 09:24

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表