SQL经典查询语句doc文档格式.doc
《SQL经典查询语句doc文档格式.doc》由会员分享,可在线阅读,更多相关《SQL经典查询语句doc文档格式.doc(9页珍藏版)》请在冰豆网上搜索。
anddeptin
(selectdeptfromemployee
whereemp_name='
)
--3、按部门进行汇总,统计每个部门的总工资
selectdept,sum(salary)
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年中有销售记录的客户编号、名称和订单总额
wherea.cust_id=b.cust_idandconvert(char(4),order_date,120)='
1997'
--9、查找一次销售最大的销售记录
selectorder_no,cust_id,sale_id,tot_amt
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.sexwhen'
m'
then'
男'
when'
f'
女'
else'
未'
end,
销售日期=isnull(convert(char(10),c.order_date,120),'
),
qty数量,qty*unit_priceas金额
fromemployeea,salesb,sale_itemc,productd
whered.prod_name='
16MDRAM'
andd.pro_id=c.prod_idand
a.emp_no=b.sale_idandb.order_no=c.order_no
--14、查找每个人的销售记录,要求显示销售员的编号、姓名、性别、
--产品名称、数量、单价、金额和销售日期
selectemp_no编号,emp_name姓名,性别=casea.sexwhen'
prod_name产品名称,销售日期=isnull(convert(char(10),c.order_date,120),'
qty数量,qty*unit_priceas金额
fromemployeealeftouterjoinsalesbona.emp_no=b.sale_id,sale_itemc,productd
whered.pro_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
groupbysale_id)b
whereb.sale_sum<
1000
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
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,