SQL打卡TASK04

本笔记为阿里云天池龙珠计划SQL训练营的学习内容,链接为:https://tianchi.aliyun.com/specials/promotion/aicampsql

-- 习题1答案
select *
from product
where sale_price>500
union
select * 
from product2
where sale_price>599;

-- 习题2答案:并集-对称差=交集
select *
from (select*
    from product
    union
    select *
    from product) as bingji
where product_id NOT IN (select *
                            from product
                            where product_id not in (select * from product2)
                            union
                            select * 
                            from product2
                            where product_id not in (select * from product));

-- 习题4答案,习题3答案在4基础上left outer join商店名on product_id
select p3.*, sp.shop_name
from (
    select p.product_type, p.product_id, p.product_name, p.sale_price,p2.max_price
    from product as p
    inner join (select product_type, max(sale_price) as max_price
                from product
                group by product_type) as p2
    on p.product_type = p2.product_type
    where sale_price=max_price )  as p3
left outer join shopproduct as sp
on p3.product_id= sp.product_id;

-- 习题5答案
select m.product_id,m.product_name,m.sale_price,x2.sum_price
from product as m
inner join(
        select product_name, sum(x.p2_price) as sum_price
        from (select p1.product_id,
                     p1.product_name,
                     p1.sale_price,
                     p2.product_id as p2_id,
                     p2.product_name as p2_name,
                     p2.sale_price as p2_price
                from product as p1
                left outer join product as p2
                on p1.sale_price >= p2.sale_price
                order by p1.sale_price,p1.product_id) as x
        group by product_name
        order by sum_price) as x2
on m.product_name=x2.product_name
order by m.sale_price;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值