博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ylb: SQL表的高级查询-子查询
阅读量:7087 次
发布时间:2019-06-28

本文共 4408 字,大约阅读时间需要 14 分钟。

ylbtech-SQL Server: SQL Server- SQL表的高级查询-子查询

 SQL Server 表的高级查询-子查询。

1,ylb:表的高级查询-子查询
--================================-- ylb:表的高级查询-子查询--    pubs库的练习-- 12/12/2011--================================use pubsgoselect * from authorsselect * from titleauthorselect * from titlesselect * from publishersselect * from storesgo--1. 查找和出版商同一州的作者姓名。select * from authors awhere state in(select state from publishers where state=a.state)goselect * from authors awhere exists(select * from publishers where state=a.state)go--2. 查找和商店同一州的作者姓名select * from authors awhere state in(select state from stores where state=a.state)go--3. 查找和商店同一城市的出版社名称select * from publishers pwhere city in (select city from stores where city=p.city)go--4. 查找写商业书的作者名select * from authorsselect * from titleauthorselect * from titlesgo--4_1,select title_id from titleswhere type='business'go--4_2,select au_id from titleauthorwhere title_id in('BU1032','BU1111','BU2075','BU7832')go--4_3,select * from authorswhere au_id in('213-46-8915','267-41-2394')go--4,结论select * from authorswhere au_id in(select au_id from titleauthorwhere title_id in(select title_id from titleswhere type='business'))go--5. 查找美国出版社出版的所有书select * from publishersselect * from titlesgo--5_1,select pub_id from publisherswhere country='USA'go--5_2,select * from titleswhere pub_id in('0877','0736')go--5结论select * from titleswhere pub_id in(select pub_id from publisherswhere country='USA')go--6. 查找美国出版社出版书的作者姓名--6_1,select pub_id from publisherswhere country='USA'go--6_2,select title_id from titleswhere pub_id in('0877','0736')go--6_3,select au_id from titleauthorwhere title_id in('BU2075','MC2222')go--6_4,select * from authorswhere au_id in('213-46-8915','712-45-1867')go--6总结select * from authorswhere au_id in(select au_id from titleauthorwhere title_id in(select title_id from titleswhere pub_id in(select pub_id from publisherswhere country='USA')))go--7. 查找在CA州出版社所出版的商业书作者姓名--7-1,select pub_id from publisherswhere state='CA'go--7-2,select title_id from  titleswhere pub_id in('1389')and [type]='business'go--7-3,select au_id from titleauthorwhere title_id in('BU1032','BU1111')go--7-4,select * from authorswhere au_id in('213-46-8915','409-56-7008')go--7总结select * from authorswhere au_id in(select au_id from titleauthorwhere title_id in(select title_id from  titleswhere pub_id in(select pub_id from publisherswhere state='CA')and [type]='business'))go--P:8. 查找和出版社在同一州的作者所写的书名--8_1,select au_id from authors awhere state in(select state from publishers where state=a.state)go--8-2,select title_id from titleauthorwhere au_id in(select au_id from authors awhere state in(select state from publishers where state=a.state))go--8-3,select * from titleswhere title_id in(select title_id from titleauthor)go--8 结论select * from titleswhere title_id in(select title_id from titleauthorwhere au_id in(select au_id from authors awhere state in(select state from publishers where state=a.state)))go--9. 查找和作者在同一城市的出版社名称select * from publishers pwhere city in(select city from authors where city=p.city)go--10. 查找单价大于所有商业书的书,它的作者姓名--方法一、--10-1,select MAX(price) from titles where type='business'go--10-2a,select title_id from titleswhere price >(select MAX(price) from titles where type='business')go--10-2b,select title_id from titleswhere price > all(select price from titles where type='business')go--10-3,select au_id from titleauthorwhere title_id in(select title_id from titleswhere price >(select MAX(price) from titles where type='business'))go--10总结select * from authorswhere au_id in(select au_id from titleauthorwhere title_id in(select title_id from titleswhere price >(select MAX(price) from titles where type='business')))go--11.   查找(Algodata Infosystems)出版社所在州,出过商业书的作者姓名--11_1,select pub_id from publisherswhere pub_name='Algodata Infosystems'go--11-2,select title_id from titleswhere type='business' and pub_id =(select pub_id from publisherswhere pub_name='Algodata Infosystems')go--11-3,select au_id from titleauthorwhere title_id in(select title_id from titleswhere pub_id =(select pub_id from publisherswhere pub_name='Algodata Infosystems'))go--11-4,select * from authorswhere au_id in(select au_id from titleauthorwhere title_id in(select title_id from titleswhere type='business' and pub_id =(select pub_id from publisherswhere pub_name='Algodata Infosystems')))
warn 作者:
出处:
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
你可能感兴趣的文章
captive portal
查看>>
mysql基本数据类型(mysql学习笔记三)
查看>>
Laravel踩坑笔记——illuminate/html被抛弃
查看>>
飞秋命令行
查看>>
做题时一时没想起来的问题总结
查看>>
[转]python-元类
查看>>
复习日记-Listener/filter/servlet3.0/动态代理
查看>>
Win7x64安装了DroidPilot-Win64.exe之后跑不起来 -- 解决办法
查看>>
VS2010 中C++ 和C# 颜色转化
查看>>
java 自带的工具
查看>>
CentOS 7配置MariaDB允许指定IP远程连接数据库
查看>>
计算机基础——指令与程序
查看>>
用三段 140 字符以内的代码生成一张 1024×1024 的图片<转载>
查看>>
HTML CSS
查看>>
sqlserver存储过程中SELECT 与 SET 对变量赋值的区别
查看>>
【分享】开源富文本编辑器之间的较量
查看>>
logback的使用和logback.xml详解
查看>>
Android Studio -- 关联源码
查看>>
leetcode Majority Element
查看>>
去除sql的前后半角全角空格
查看>>