sql2000学习笔记之子查询

子查询是一种查询的嵌套。这里不会使用新的方法或关键字,而是利用前面所描述的关键字和方法,构造一种嵌套型的语法结构。

1、需要一张表,包含:每笔交易的交易号,交易时间,以及此笔交易中最大的交易单价

分析:结果集需要三列,其中交易号和交易时间可以从orders表中直接获取,然而最大交易单价则需要使用子查询(查询的嵌套)。初始的思路是:

select orderid ,orderdate,MaxUnitPrice from orders

此时MaxUnitPrice的结果是未知的,必须使用一个子查询,这个子查询的形式应该如下:

select max(orddet.unitprice)

from [order details] orddet

where orders.orderid=orddet.orderid

这个子查询产生的结果是MaxUnitPrice列的一个单元格数据。

完整的答案就是:

select orderid ,orderdate,

(select max(orddet.unitprice)

from [order details] orddet

where od.orderid=orddet.orderid) MaxUnitPrice

from orders od

2、需要一张表,包含:书的名称、价格、所有书的平均价格、各书价格与平均价格的差价

分析:结果集中会有四列,其中书的名称、价格可直接在titles表中直接获得,平均价格和差价则要通过子查询完成。

答案:

select title,price,

(select avg(price) from titles),

(price-(select avg(price) from titles))

from titles

第一个子查询完成平均价格的查询,第二个子查询得出差价。

3、需要一张表,包含:产品名称和销售总量

分析:牵涉到products和order details两张表,这里应使用数据源中的子查询。初始的代码应该是这样:

select productname,sum(quantity)

from (子查询)

group by productname

子查询应该是这样:

select od.orderid,od.productid,od.quantity,p.productname

from [order details] od

inner join products p

on od.productid=p.productid

从子查询中利用productid公共列得出一张表,表中含有quantity和productname这两个我们需要的列。之后,便是在这张表中进行筛选。

答案:

select i.productname,sum(i.quantity)

from

(select od.productid,od.quantity,p.productname

from [order details] od

inner join products p

on od.productid=p.productid) i

group by i.productname

当然这题还可以用另外一种结构去解决,考虑如下代码:

select p.productname,i.total

from products p

inner join

(select productid,sum(quantity) total

from [order details]

group by productid) as i

on p.productid=i.productid

这种解法的主体是inner join结构,从子查询中得出的表i是productid和 total组成的,之后再利用inner join将products和i关联查询,条件是productid相等。

4、需要一张表,包含:居住在algodata infosystems所在城市的作者的名字

分析:牵涉到authors和publishers两张表,需要使用条件中嵌套子查询。

初始的代码:

select au_lname,au_fname

from authors

where city=(子查询)

显然,子查询应该这样写:

select city from publishers where pub_name='algodata infosystems'

这条子查询,结果为'algodata infosystems'出版社所在的城市。

显然,答案应该是:

select au_lname,au_fname

from authors

where city=(select city from publishers where pub_name='algodata infosystems')

5、需要一张表,包含:所有价格高于当前最低价的书籍名称

分析:很明显这题也是在条件中进行子查询,条件就是price大于所有书籍的最低价,因此子查询需要找出书籍的最低价格。

答案:

select title

from titles

where price>(select min(price) from titles)

6、需要一张表,包含:价格高于类型为"trad_cook"的书籍的最低价格的书

分析:结果集中包含title一列,筛选的条件为价格高于x,x为"trad_cook"类型书籍中的最低价格。

答案:

select title

from titles

where price>(select min(price) from titles where type='trad_cook')

7、需要一张表,包含:瑞典供应商的所有产品

分析:牵涉到suppliers和products表,它们的公共列为supplierid。在子查询中,我们必须找到country的值为sweden的供应商ID。

答案:

select productid,supplierid,productname

from products

where supplierid in

(select supplierid from suppliers where country='sweden')

这里使用了in关键字,因为瑞典的供应商不止一家,必须使用in关键字在多个值中筛选。

8、需要一张表,包含:没有出版过商业类书籍的出版商名称

分析:牵涉到titles和publishers两张表,公共列为pub_id。在子查询中,我们必须找到出版过商业类书籍的出版商id,然后利用not in筛选

答案:

select pub_name

from publishers

where pub_id not in

(select pub_id from titles where type='business')

9、需要一张表,包含:与产品sir rodney's scones价格相同的产品名称

分析:子查询中需要检索到sir rodney's scones的价格

答案:

select productname

from products

where unitprice=(select unitprice from products where productname='sir rodney''s scones')

注意红色标记的双引号,如果在字符串中出现单引号,则要用双引号代替,以免和字符串外的单引号冲突。

当然,这题也可以用自联接来完成,答案是:

select productname

from products p1

inner join products p2

on p1.unitprice=p2.unitprice

where p2.productname='sir rodney''s scones'

10、需要一张表,包含:至少曾参与编写一本热门计算机书的作者名称

分析:最终的结果需要作者名称,而筛选条件是热门计算机书中,有作者的ID。所以牵涉到titles,authors,titleauthors。

先从titles中找出热门计算机书的ID,再从titleauthors中找出相关作者id,最后从authors中找出作者名称。这里使用了三重的嵌套。

答案:

select au_lname,au_fname from authors

where au_id in//从authors中找出作者名称

(select au_id from titleauthor where title_id in//从titleauthors中找出相关作者id

(select title_id from titles where type='popular_comp'))  //从titles中找出热门计算机书的ID

你可能感兴趣的:(JOIN,sql,出版,产品)