本笔记为阿里云天池龙珠训练SQL训练营的学习内容链接为“https://tianchi.aliyun.com/forum/postDetail?spm=5176.20222307.J_9059755”

本文探讨了SQL查询中的多个高级用法,包括使用UNION结合条件筛选从两个产品表中选取高价位商品,找出两个表中不共有的产品,以及通过嵌套查询和左连接找到每个产品类型最高售价的商品。同时,还涉及到了查找每个价格段内销售总额最高的商品以及展示商品的销售价格分布情况。这些内容展示了SQL在处理复杂数据操作时的灵活性和强大功能。
摘要由CSDN通过智能技术生成

练习题解答如下:

4.1

select * 

from product

where sale_price >= 500   union

select * 

from product2

where sale_price >=500;

4.2

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  s.product_id, shop_name, product_type

from (

select product_id, m.product_type

from (select * from product union select * from product2)  all_product

join (

select max(sale_price) max, product_type 

from (select * from product union select * from product2) all_product

group by product_type

 )  on all_product.product_type = m.product_type

where sale_price = max

 ) id

left join ShopProduct s

on id.product_id = s.product_id;

本题涉及到将表左连接和嵌套,不容易理解。

4.4
select * 

from (select * from product union select * from product2) p1

where sale_price = (

select max(sale_price)

from (select * from product union select * from product2) p2

where p2.product_type = p1.product_type

group by product_type

);

代码需完善

4.5

select product_id, product_name, sale_price,(

slelct sum( sale_price ) 

from product p2 

where p2.sale_price<=p.sale_price ) s_sale 


from  product p 

order by  sale_price;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值