练习题
4.1
找出 product 和 product2 中售价高于 500 的商品的基本信息。
select * from product where sale_price > 500
UNION
select * from product2 where sale_price > 500
4.2
借助对称差的实现方式, 求product和product2的交集。
select * from product where product_id not in(select product_id from product2)
UNION
select * from product2 where product_id not in(select product_id from product)
4.3
每类商品中售价最高的商品都在哪些商店有售 ?
select product_type,max(sale_price) from product group by product_type
union
select product_type,max(sale_price) from product2 where product_id not in(select product_id from product)
group by product_type having max(sale_price)>(select max(sale_price) from product)
4.4
分别使用内连结和关联子查询每一类商品中售价最高的商品。
select p1.product_id, p1.product_name,p1.product_type,p1.sale_price,p2.maxprice from product as p1 INNER JOIN (select product_type,max(sale_price) as maxprice from product group by product_type) as p2
on p1.product_type=p2.product_type where p2.maxprice>p1.sale_price;
4.5
用关联子查询实现:在product表中,取出 product_id, produc_name, slae_price, 并按照商品的售价从低到高进行排序、对售价进行累计求和。
select product_id, product_name,sale_price,sum(sale_price) sumprice from product order by sale_price