Task04:集合运算-表的加减法和join等-天池龙珠计划SQL训练营

本文通过SQL查询演示了如何在product和product2表中找出售价高于500的商品,计算两表的交集,找出每类商品中售价最高的商品及其商店分布,并使用内连接和关联子查询找到各商品类别的最高价商品。同时展示了如何按价格排序并累计求和。
摘要由CSDN通过智能技术生成

练习题

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
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值