国语自产精品视频在线看:您身边最放心的安全下载站! 最新软件|热门排行|软件分类|软件专题|厂商大全

国语自产精品视频在线看

技术教程
您的位置:首页数据库类SQL Server → sql 多表查询 Select语句查询实例分析

sql 多表查询 Select语句查询实例分析

我要评论 2012/01/29 12:19:05 来源:国语自产精品视频在线看 编辑:downcc.com [ ] 评论:0 点击:469次

--1、查找员【chá zhǎo yuán】🥎工的编号【hào】🅿、姓名【xìng míng】💶、部门和出生日【chū shēng rì】期,如果出😡生日期为空值,
--显示日【xiǎn shì rì】👲期不详,并按部门排序输出📷,日期格式为yyyy-mm-dd。
select emp_no ,emp_name ,dept ,
isnull(convert(char(10),birthday,120),'日期不【rì qī bú】详') birthday
from employee
order by dept

--2、查找与【chá zhǎo yǔ】喻自强【yù zì qiáng】🐤在同一个单位【gè dān wèi】的员工姓名💗、性别、部门和🏫职称
select emp_no,emp_name,dept,title
from employee
where emp_name<>'喻自强【yù zì qiáng】🐤' and dept in
(select dept from employee
where emp_name='喻自强【yù zì qiáng】🐤')

--3、按部门💫进行汇【jìn háng huì】🎎总,统计每个部门【gè bù mén】的总工🚰资【zī】
select dept,sum(salary)
from employee
group by dept

--4、查找商品名称【pǐn míng chēng】为14寸显示【cùn xiǎn shì】🔮器【qì】商品👳的销售😌情况,
--显示该【xiǎn shì gāi】商品的♐编号、销售数量、单价和【dān jià hé】🕑金额
select a.prod_id,qty,unit_price,unit_price*qty totprice
from sale_item a,product b
where a.prod_id=b.prod_id and prod_name='14寸显示【cùn xiǎn shì】🔮器【qì】'

--5、在销售🙏明细表【míng xì biǎo】中按产【zhōng àn chǎn】🚰品编号进行汇【jìn háng huì】总【zǒng】❗,统计每种产品的销售😪数量和金额
select prod_id,sum(qty) totqty,sum(qty*unit_price) totprice
from sale_item
group by prod_id

--6、使用convert函数按客户编【kè hù biān】🤔号统计【hào tǒng jì】每个客📷户【hù】🏎1996年的订单总金额
select cust_id,sum(tot_amt) totprice
from sales
where convert(char(4),order_date,120)='1996'
group by cust_id

--7、查找有销售记【xiāo shòu jì】录的客😿户编号、名称和🕎订单总【dìng dān zǒng】🗣额【é】
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年中有销售记录的客【lù de kè】🎮户编号【hù biān hào】、名称和🏇订单总【dìng dān zǒng】😲额
select a.cust_id,cust_name,sum(tot_amt) totprice
from customer a,sales b
where a.cust_id=b.cust_id and convert(char(4),order_date,120)='1997'
group by a.cust_id,cust_name

--9、查找一次销售【cì xiāo shòu】最大的【zuì dà de】🤥销售记录🚳
select order_no,cust_id,sale_id,tot_amt
from sales
where tot_amt=
(select max(tot_amt)
from sales)

--10、查找至少有【shǎo yǒu】🚴3次销售【cì xiāo shòu】的业务🎚员名单🛍和销售【hé xiāo shòu】日期
select emp_name,order_date
from employee a,sales b
where emp_no=sale_id and a.emp_no in
(select sale_id
from sales
group by sale_id
having count(*)>=3)
order by emp_name

--11、用存在【yòng cún zài】量词查🍷找没有订货记录的客【lù de kè】🧑户名称
select cust_name
from customer a
where not exists
(select *
from sales b
where a.cust_id=b.cust_id)

--12、使用左【shǐ yòng zuǒ】外连接【wài lián jiē】😩查找每个客户【gè kè hù】🙊的客户编号🔆、名称、订货日期、订单金【dìng dān jīn】额【é】
--订货日期不要【qī bú yào】🎤显示时间【jiān】,日期格【rì qī gé】式为yyyy-mm-dd
--按客户🍉编号🔆排序,同一客👦户再按🅱订单降🗂序排序输出
select a.cust_id,cust_name,convert(char(10),order_date,120),tot_amt
from customer a left outer join sales b on a.cust_id=b.cust_id
order by a.cust_id,tot_amt desc

--13、查找16M DRAM的销售情况,要求显示相应的销售员的姓🐦名【míng】、
--性别【bié】🛄,销售日【xiāo shòu rì】期【qī】、销售数📌量和金【liàng hé jīn】🎽额🏢,其中性别【bié】🛄用男【nán】🚸、女【nǚ】表示
select emp_name 姓名【xìng míng】, 性别【bié】🛄= case a.sex when 'm' then '男【nán】🚸'
when 'f' then '女【nǚ】'
else '未【wèi】'
end,
销售日【xiāo shòu rì】期【qī】= isnull(convert(char(10),c.order_date,120),'日期【qī】不🥃详📣'),
qty 数量, qty*unit_price as 金额🏢
from employee a, sales b, sale_item c,product d
where d.prod_name='16M DRAM' and d.prod_id=c.prod_id and
a.emp_no=b.sale_id and b.order_no=c.order_no

--14、查找每个人的【gè rén de】销售记录【lù】🗿,要求显【yào qiú xiǎn】示销售员的编号🕖、姓名【xìng míng】📏、性别【xìng bié】📨、
--产品名【chǎn pǐn míng】称【chēng】🙃、数量🤪、单价🌡、金额和🌈销售日🌍期⛓
select emp_no 编号🕖,emp_name 姓名【xìng míng】📏, 性别【xìng bié】📨= case a.sex when 'm' then '男【nán】'
when 'f' then '女'
else '未【wèi】'
end,
prod_name 产品名【chǎn pǐn míng】称【chēng】🙃,销售日🌍期⛓= isnull(convert(char(10),c.order_date,120),'日期⛓不详'),
qty 数量🤪, qty*unit_price as 金额
from employee a left outer join sales b on a.emp_no=b.sale_id , sale_item c,product d
where d.prod_id=c.prod_id and b.order_no=c.order_no

--15、查找销【chá zhǎo xiāo】😫售金额最大的【zuì dà de】客户名🦖称和总货款
select cust_name,d.cust_sum
from customer a,
(select cust_id,cust_sum
from (select cust_id, sum(tot_amt) as cust_sum
from sales
group by cust_id ) b
where b.cust_sum =
( select max(cust_sum)
from (select cust_id, sum(tot_amt) as cust_sum
from sales
group by cust_id ) c )
) d
where a.cust_id=d.cust_id

--16、查找销售总额【shòu zǒng é】少于👏1000元的销🈂售员编【shòu yuán biān】号、姓名和销售额【xiāo shòu é】😘
select emp_no,emp_name,d.sale_sum
from employee a,
(select sale_id,sale_sum
from (select sale_id, sum(tot_amt) as sale_sum
from sales
group by sale_id ) b
where b.sale_sum <1000
) d
where a.emp_no=d.sale_id

--17、查找至少销售👀了3种商品🏠的客户编号【hào】、客户名【kè hù míng】称【chēng】💇、商品编号【hào】、商品名【shāng pǐn míng】称【chēng】💇、数量和🌿金额😀
select a.cust_id,cust_name,b.prod_id,prod_name,d.qty,d.qty*d.unit_price
from customer a, product b, sales c, sale_item d
where a.cust_id=c.cust_id and d.prod_id=b.prod_id and
c.order_no=d.order_no and a.cust_id in (
select cust_id
from (select cust_id,count(distinct prod_id) prodid
from (select cust_id,prod_id
from sales e,sale_item f
where e.order_no=f.order_no) g
group by cust_id
having count(distinct prod_id)>=3) h )

--18、查找至【chá zhǎo zhì】少与世界技术🍤开发公【kāi fā gōng】司销售【sī xiāo shòu】相同的🐿客户编号【hào】🤨、名称和【míng chēng hé】🐧商品编号【hào】🤨、商品名🔱称😄、数量和金额【jīn é】
select a.cust_id,cust_name,d.prod_id,prod_name,qty,qty*unit_price
from customer a, product b, sales c, sale_item d
where a.cust_id=c.cust_id and d.prod_id=b.prod_id and
c.order_no=d.order_no and not exists
(select f.*
from customer x ,sales e, sale_item f
where cust_name='世界技术🍤开发公【kāi fā gōng】司' and x.cust_id=e.cust_id and
e.order_no=f.order_no and not exists
( select g.*
from sale_item g, sales h
where g.prod_id = f.prod_id and g.order_no=h.order_no and
h.cust_id=a.cust_id)
)

19、查找表【chá zhǎo biǎo】😥中所有姓刘【liú】🕶的职工【de zhí gōng】的工号,部门,薪水🐰
select emp_no,emp_name,dept,salary
from employee
where emp_name like '刘【liú】🕶%'

20、查找所【chá zhǎo suǒ】有定单【yǒu dìng dān】金额高🏇于2000的所有客户编【kè hù biān】😠号🏇
select cust_id
from sales
where tot_amt>2000

21、统计表【tǒng jì biǎo】中员工【zhōng yuán gōng】的薪水🏒在4000-6000之间的【zhī jiān de】✝人数🏅
select count(*)as 人数🏅
from employee
where salary between 4000 and 6000

22、查询【xún】表中的同一部门的职工的平均💐工资🎻,但只查【dàn zhī chá】🥝询【xún】"住址"是【shì】"上海市【shàng hǎi shì】🍚"的员工
select avg(salary) avg_sal,dept
from employee
where addr like '上海市【shàng hǎi shì】🍚%'
group by dept

23、将表中住址为【wéi】"上海市🍁"的员工【de yuán gōng】住址改🕘为【wéi】"北京市【běi jīng shì】🛅"
update employee
set addr like '北京市【běi jīng shì】🛅'
where addr like '上海市🍁'

24、查找业🔸务部或【wù bù huò】会计【huì jì】部的女员👔工的基本信息【běn xìn xī】。
select emp_no,emp_name,dept
from employee
where sex='F'and dept in ('业务💀','会计【huì jì】')

25、显示每种产品【zhǒng chǎn pǐn】💍的销售👰金额总【jīn é zǒng】和,并依销【bìng yī xiāo】💣售金额由大到小输出。
select prod_id ,sum(qty*unit_price)
from sale_item
group by prod_id
order by sum(qty*unit_price) desc

26、选取编【xuǎn qǔ biān】号✒界于'C0001'和【hé】'C0004'的客户编号【biān hào】✒、客户名称🍛、客户地址👿。
select CUST_ID,cust_name,addr
from customer
where cust_id between 'C0001' AND 'C0004'

27、计算出一共销🤡售了几【shòu le jǐ】种产品【zhǒng chǎn pǐn】。
select count(distinct prod_id) as '共销售🎩产品数'
from sale_item

28、将业务部员工🦗的薪水【de xīn shuǐ】上调【shàng diào】✖3%。
update employee
set salary=salary*1.03
where dept='业务'

29、由😟employee表中查找出薪水最低【shuǐ zuì dī】的员工【de yuán gōng】🕜信息。
select *
from employee
where salary=
(select min(salary )
from employee )

30、使用【shǐ yòng】join查询客🙈户姓名为"客户丙"所购货【suǒ gòu huò】物的【wù de】"客户名称","定单金【dìng dān jīn】额🧔","定货日【dìng huò rì】期","电话号码👗"
select a.cust_id,b.tot_amt,b.order_date,a.tel_no
from customer a join sales b
on a.cust_id

关键词👡:sql,多表查询🚱,Select语句【yǔ jù】😭

阅读本文后您有什么感想? 已有 人给出评价!

  • 0 欢迎喜欢
  • 0 白痴
  • 0 拜托
  • 0 哇
  • 0 加油
  • 0 鄙视