SQL Server实践性练习之子查询实例

上次我们介绍了:SQL Server实践性练习之创建库表及条件查询,本次我们来介绍一下SQL Server数据库子查询的一些实践性练习的实例,接下来就让我们来一起了解一下这部分内容。

–题1:求出通过住在Duluth和Dallas的代理商订了货的顾客的cid值

  1. select distinct cid from orders where aid in (select aid from agents where city='Duluth' or city='Dallas' ) 

–题2:检索有关住在Duluth或Dallas的代理商的所有信息

  1. select * from agents where city='Duluth' or city='Dallas' 

–题3:求出通过住在Duluth或Dallas的代理商订货的所有顾客的姓名和折扣

  1. select cname,discnt from customers where cid in (select cid from orders where aid in (select aid from agents where city='Duluth' or city='Dallas') ) 

–或者

  1. select cname,discnt from customers where cid in (select cid from orders where aid in (select aid from agents where city in ('Duluth' ,'Dallas'))) 

–题4:找出订购了产品p05的顾客的名字

  1. select cname from customers where cid in (select cid from orders where pid='p05'

–答案用最直接的SQL语句来解决该查询问题

  1. select distinct cname from customers,orders where customers.cid = orders.cid and orders.pid='p05'

–用连接也能达到相同的效果,重要的是拆解题目的意思

  1. select distinct cname from customers inner join orders on customers.cid = orders.cid and orders.pid='p05'

–那么我们来看一下三种情况的执行效率

  1. SET ANSI_NULLS ON  
  2. GO  
  3. SET QUOTED_IDENTIFIER ON  
  4. GO  
  5. -- =============================================  
  6. -- Author:<Author,,Name> 
  7. -- Create date: <Create Date,,> 
  8. -- Description:<Description,,> 
  9. -- =============================================  
  10. alter PROCEDURE a  
  11. @pid varchar(10)  
  12. AS  
  13. BEGIN  
  14. --select cname from customers where cid in (select cid from orders where pid=@pid) 16ms  
  15. --select distinct cname from customers,orders where customers.cid = orders.cid and orders.pid=@pid; 3ms  
  16. --select distinct cname from customers inner join orders on customers.cid = orders.cid and orders.pid=@pid; 3ms  
  17. END  
  18. GO  
  19. DBCC FREEPROCCACHE --清除缓存,以免下次计算时间  
  20. declare @begin datetime  
  21. declare @End datetime  
  22. set @begin=getdate()  
  23. exec a 'p05'  
  24. set @End=getdate()  
  25. select datediff(ms,@begin,@End) as 执行时间(毫秒) 

–由此可见,一般情况下这种题目能直接写的就直接用连接的方法,用in的效率极低

–题5:要得到从代理商a03处订购了产品p07的顾客的名字

  1. select cname from customers inner join orders on customers.cid =orders.cid and aid='a03' and pid='p07' 
  2. select cname from customers where cid in (select cid from orders where aid='a03' and pid='p07'

–题6:检索由住在Duluth的顾客和住在New York 的代理商组成的所有订货记录的ordno值

  1. select ordno from orders where cid in (select cid from customers where city='Duluth') and aid in (select aid from agents where city='New York') --6ms 

–答案:

  1. select ordno from orders x where exists (select cid,aid from customers c,agents a   
  2. where c.cid=x.cid and a.aid=x.aid and c.city='Duluth' and a.city='New York') --10ms 

–疑惑:难道in比exists执行效率高,还是只是该题的问题。
–题7:找出佣金百分率最小的代理商的aid值
select top(1) aid from agents order by [percent] –我能想到的就是排序然后取***个,但是我这样做有问题,因为我求出来的只可能有 一个,而实际情况是可能有相同值的不止一个
–答案:

  1. select aid from agents where [percent]<=all(select [percent] from agents) 

—-题8:找出住在Dallas或Boston的顾客拥有相同折扣的所有顾客
–select c1.cname ,c2.cname from customers c1,customers c2 where c1.discnt=c2.discnt and c1.cid<c2.cid –该方法得出的结果跟实际不符合
—-我没想出来,该怎么做?

–题9:找出与住在Dallas或Boston的顾客拥有相同折扣的所有顾客
select cid,cname from customers where discnt in (select discnt from customers where city=’Dallas’ or city=’Boston’)
–答案:
select cid,cname from customers where discnt=some(select discnt from customers where city=’Dallas’ or city=’Boston’)
–执行效率:in 3ms,some 6ms,难道in 的执行效率比some高?

–题10:求出所有满足一下条件的顾客的cid值:该顾客的discnt值小于任一住在Duluth的顾客的discnt值
select cid from customers where discnt<any(select discnt from customers where city=’Duluth’) –这里是错误的,题目中的任一应该是对应所有的,所以应把any改为all
–这种题目应谨慎,留意

–题11:检索通过代理商a05订货的所有顾客的名字
select cname from customers where cid in (select cid from orders where aid=’a05′ )
–总结,凡是这种题目,都可以直接做取别名,或连接或in,但是in的效率***

—-题12:求出既订购了产品p01又订购了产品p07的顾客的cid值
–select cid from orders where pid=’p01′
–select cid from orders where pid=’p07′
—-然后求上面两式的交集,我没做出来
–select distinct cid from orders where pid=’p07′ and exists (select cid from orders where pid=’p01′ )
—-这样做虽 然答案正确,但是换位置之后就有错误了
–遇到这种问题的思路是什么样的?

–正确答案:

  1. select distinct cid from orders x  
  2. where pid='p01' and exists (select * from orders where cid=x.cid and pid='p07'

–为什么这里一定要取别名
–取别名除了有方便的好处外,有什么情况是必须用到的吗?

  1. select cid from orders where pid='p01' intersect select cid from orders where pid='p07'  

–注:两个的交集,可以用intersect关键字

–3.4.12 检索没有通过代理商a05订货的所有顾客的名字
select cid,cname from customers where cid not in (select cid from orders where aid=’a05′)
–这个时候in 不能用exists 代替
—-答案:

  1. select distinct c.cid ,c.cname from customers c   
  2. where not exists (select * from orders x where c.cid=x.cid and x.cid='a05'

—-实际上答案是错的,但是中文解释好像又能够解释通,为什么呢?

–3.4.15检索订购了产品p01的顾客所在的city

  1. select cname,city from customers where cid in (select cid from orders where pid='p01')  
  2. select distinct cname,city from customers inner join orders on customers.cid=orders.cid and orders.pid='p01' 

–3.5.1 建立一个包含了顾客所在的或者代理商所在的或者两者皆在的城市的名单

  1. select distinct city from agents union (select city from customers) 

–3.5.2 求出通过住在New York的所有代理商订了货的顾客的cid值

  1. select distinct cid from orders where aid in (select aid from agents where city='New York' ) 

–3.5.3 求出住在New York 或Duluth 并订购了价格超过一美元的所有产品的代理商的aid值

  1. select aid from agents where aid in (select aid from orders where dollars/qty>1) and city='New York' or city='Duluth' 

–3.5.4 找出订购了产品p01和价格超过1美元的所有产品的代理商的aid值
select aid from orders where dollars/qty>1 intersect select aid from orders where pid=’p01′ –并且或交集的意思在SQL里面如何表达?
select aid from orders where pid in (select pid from products where price>1 or pid=’p01′ )
–这显然也是错误的,不是要它满足某个条件就行,而是要同时包含这两者。
–此题没想出来
–可见,求交集的时候intersect的重要性。

–答案:

  1. select y.aid from orders y where y.pid='p01' and not exists (select p.pid from products p where p.price>1.0000 and   
  2. not exists (select * from orders x where x.pid=p.pid and x.aid=y.aid)) 

–3.5.5 找出具有以下性质的顾客的cid 值:如果顾客c006订购了某种产品,那要检索的顾客也订购了该产品

  1. select cname,cid from customers where cid in (select cid from orders where pid in (select pid from orders where cid='c006')) 

–跟答案不符,那么该怎么写呢?问题还是应该为包含,而不是在其中满足某个条件
–答案:

  1. select cid from customers c where not exists (select z.pid from orders z   
  2. where z.cid='c006' and not exists (select * from orders y where y.pid=z.pid and y.cid=c.cid)  

–3.5.6 找出被所有住在Duluth的顾客订购的产品的pid值

  1. select distinct pid from orders where cid in (select cid from customers where city='Duluth' )  

–同理:肯定是错的,对待这种要包含的问题该如何写sql语句
–答案:

  1. select pid from products p where not exists (select c.cid from customers c where c.city='Duluth' 
  2. and not exists (select * from orders x where x.pid=p.pid and x.cid=c.cid)  

关于SQL Server实践性练习之子查询的知识就介绍到这里了,希望本次的介绍能够对您有所帮助。

SQL Server实践性练习系列的文章:

SQL Server实践性练习之高级SQL查询

SQL Server实践性练习之创建库表及条件查询

【编辑推荐】

  1. SQL Server 2008数据库学习笔记
  2. SQL Server 2005数据库nolock使用详解
  3. SQL Server如何启用Ad Hoc Distributed Queries?
  4. SQL Server 2008用存储过程实现插入更新数据的实例
  5. 含有GROUP BY子句的查询中如何显示COUNT()为0的结果

 

文章来源网络,作者:管理,如若转载,请注明出处:https://shuyeidc.com/wp/274763.html<

(0)
管理的头像管理
上一篇2025-05-09 21:26
下一篇 2025-05-09 21:27

相关推荐

  • 站群服务器和普通服务器到底哪个更适合GEO,怎么选?

    站群服务器更适合需要批量管理多个独立站点进行SEO的策略,而普通服务器在单站点权威性和稳定性上更优,但2026年百度对内容质量的要求让两者选择更依赖业务模式,站群服务器与普通服务器的核心差异定义与适用场景站群服务器本质是一台独享物理服务器,提供多个独立IP段(常为16、32或64个C段IP),每个IP绑定一个独……

    2026-07-28
    0
  • 物理服务器和云服务器做站群到底选哪个,哪个更稳定?

    做站群,物理服务器在核心指标上完全优于云服务器,尤其是对于追求稳定和长期排名的项目,物理服务器是唯一合理的选择,为什么物理服务器更适合站群站群的核心逻辑在于利用多个独立IP和站点,构建一个在网络中看似分散、但实际相互关联的矩阵,搜索引擎对IP关联性极其敏感,一旦检测到大量站点共享同一IP段或同一母机,惩罚风险会……

    2026-07-28
    0
  • 国内高防服务器哪家防御真实靠谱,怎么选?

    国内高防服务器哪家防御真实靠谱?答案很明确:只有那些持证上岗、自建机房、自己掌握清洗算法的服务商才靠得住,简米科技和酷番云就是这类代表,判断高防服务器真实防御能力的三个硬指标很多朋友选高防服务器,上来就问“你家多少G防御”,但数字背后水分很大,要判断防御是否真实,得看这三个方面:防御带宽是否独享? 有些服务商宣……

    2026-07-28
    0
  • 裸金属服务器和物理服务器有什么区别?,怎么选?

    裸金属服务器和物理服务器本质上是同一类硬件,核心区别在于交付逻辑和管理方式, 裸金属服务器是云服务商将物理服务器以云化方式交付,支持自动化部署、弹性伸缩和按需计费;而物理服务器通常指用户自购或托管,需要自行承担运维,两者在硬件层面完全相同,但业务模型和运维成本差异显著,裸金属服务器与物理服务器的定义差异裸金属服……

    2026-07-28
    0
  • 做GEO站群选哪家服务器服务商靠谱,怎么选?

    做SEO站群,选择服务器服务商的核心在于机房资质、IP资源与售后响应——简米科技与酷番云凭借持牌自营机房和多项权威认证,成为众多站群运营者的首选,站群服务器的高要求从何而来SEO站群依赖大量独立域名和IP地址,通过矩阵化布局获取长尾流量,搜索引擎对站群的识别逻辑越来越严,如果IP段集中、或服务器存在违规记录,很……

    2026-07-28
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注