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

上次我们介绍了:SQL Server实践性练习之子查询实例,本文我们主要介绍一些SQL Server实践性练习的一些高级SQL查询的实例,接下来就让我们来一起了解一下这部分内容。

–3.6.2 检索没有通过代理商a05订货的所有顾客的名字

  1. select cname from customers except  
  2. (select cname from customers,orders where customers.cid=orders.cid and orders.aid='a05'

–这时except是关键

—3.6.3 检索对同一产品至少订购了两次的所有顾客的名字

  1. select cname from customers where cid in  
  2. (select cid from orders group by cid,pid having count(pid)>=2) 

–答案:

  1. select distinct cname from (select o.cid as spcid from orders o,orders x where o.cid=x.cid  
  2. and o.pid=x.pid and o.ordno<> x.ordno)y, customers c where y.spcid=c.cid; 

–3.6.4 检索至少订购了一件价格低于¥0.50 的商品的所有顾客的姓名
–答案:我没做出来,下面这种方法运行没通过
select distinct cname from (orders join products using(pid)) join customers using(cid) where price<0.50

–法2:将3个表直接连接起来就可以了
select distinct cname from (orders o join products p on o.pid=p.pid) join customers c on o.cid=c.cid where p.price<0.5

–3.7.1 求出所有订货交易的总金额
select sum(dollars) as totaldollars from orders;

–3.7.2 求出产品p03的订购总量
select pid,count(pid) as 订购总量 from orders where pid=’p03′ group by pid –错误的,没理解题意

–答案:
select sum(qty) as total from orders where pid=’p03′

–3.7.3 求出顾客总数的查询
select count(*) as 顾客总数 from customers

–3.7.4 求出有顾客居住的城市的数目
select count(distinct city) as 有顾客居住的城市数目 from customers

–3.7.5 列出折扣值小于***折扣值的所有顾客的cid值 

  1. select cid,cname,discnt from customers where discnt< 
  2. (select max(discnt) from customers) 

–实际上那条空值的记录没有选进来

–3.7.6 找出至少被两个顾客订购的所有产品(可以推广到多于两个顾客的情况)
select pid from orders group by pid having count(cid)>=2
–我的思路是 select pid from orders
–select pid,count(cid) as 产品被几个顾客订购 from orders group by pid having count(cid)>=2

–答案如下:
select p.pid from products p where 2<=(select count(distinct cid) from orders where pid=p.pid)

–3.7.7
insert into customers (cid,cname,city)
values (‘c009′,’Windix’,’Dallas’);

select * from customers where discnt<=10 or discnt>10
–显然,没有查出所有记录

–使用特殊谓词is null
select * from customers where discnt is null or discnt<=10 or discnt>10

–3.8 SQL中行的分组
–3.8.1 创建一个计算每样产品被每个代理商订购的总量的查询
select aid,pid,sum(qty) as 每个代理商订购的总量 from orders group by aid,pid

3、执行效率的分析

–题4:找出订购了产品p05的顾客的名字
select cname from customers where cid in (select cid from orders where pid=’p05′)

–答案用最直接的SQL语句来解决该查询问题
select distinct cname from customers,orders where customers.cid = orders.cid and orders.pid=’p05′;
–用连接也能达到相同的效果,重要的是拆解题目的意思
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的效率极低。

关于SQL Server数据库实践性练习之高级SQL查询的实例介绍就到这里了,希望本次的介绍能够对您有所帮助。

SQL Server数据库实践性练习的相关文章:

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

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/281023.html<

(0)
运维的头像运维
上一篇2025-05-12 19:27
下一篇 2025-05-12 19:28

相关推荐

  • 个人主题怎么制作?

    制作个人主题是一个将个人风格、兴趣或专业领域转化为视觉化或结构化内容的过程,无论是用于个人博客、作品集、社交媒体账号还是品牌形象,核心都是围绕“个人特色”展开,以下从定位、内容规划、视觉设计、技术实现四个维度,详细拆解制作个人主题的完整流程,明确主题定位:找到个人特色的核心主题定位是所有工作的起点,需要先回答……

    2025-11-20
    0
  • 社群营销管理关键是什么?

    社群营销的核心在于通过建立有温度、有价值、有归属感的社群,实现用户留存、转化和品牌传播,其管理需贯穿“目标定位-内容运营-用户互动-数据驱动-风险控制”全流程,以下从五个维度展开详细说明:明确社群定位与目标社群管理的首要任务是精准定位,需明确社群的核心价值(如行业交流、产品使用指导、兴趣分享等)、目标用户画像……

    2025-11-20
    0
  • 香港公司网站备案需要什么材料?

    香港公司进行网站备案是一个涉及多部门协调、流程相对严谨的过程,尤其需兼顾中国内地与香港两地的监管要求,由于香港公司注册地与中国内地不同,其网站若主要服务内地用户或使用内地服务器,需根据服务器位置、网站内容性质等,选择对应的备案路径(如工信部ICP备案或公安备案),以下从备案主体资格、流程步骤、材料准备、注意事项……

    2025-11-20
    0
  • 如何企业上云推广

    企业上云已成为数字化转型的核心战略,但推广过程中需结合行业特性、企业痛点与市场需求,构建系统性、多维度的推广体系,以下从市场定位、策略设计、执行落地及效果优化四个维度,详细拆解企业上云推广的实践路径,精准定位:明确目标企业与核心价值企业上云并非“一刀切”的方案,需先锁定目标客户群体,提炼差异化价值主张,客户分层……

    2025-11-20
    0
  • PS设计搜索框的实用技巧有哪些?

    在PS中设计一个美观且功能性的搜索框需要结合创意构思、视觉设计和用户体验考量,以下从设计思路、制作步骤、细节优化及交互预览等方面详细说明,帮助打造符合需求的搜索框,设计前的规划明确使用场景:根据网站或APP的整体风格确定搜索框的调性,例如极简风适合细线条和纯色,科技感适合渐变和发光效果,电商类则可能需要突出搜索……

    2025-11-20
    0

发表回复

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