SQL训练营--Task04:集合运算-表的加减法和join等

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


前言

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


一、union 和union all 的区别?

UNION 操作符用于合并两个或多个 SELECT 语句的结果集。

请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT
语句中的列的顺序必须相同。

-- union用法
SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2
-- SQL UNION ALL 语法
SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2

union和union all 的区别:union消除重复行,union all包含重复行。
另外,UNION 结果集中的列名总是等于 UNION 中第一个 SELECT 语句中的列名。

其实就是教程中说的隐式类型转换,如下:

SELECT product_id, product_name, '1'
FROM product
UNION
SELECT product_id, product_name,sale_price
FROM product2;

结果如下:
在这里插入图片描述

二、内联结和外联结

在这里插入图片描述

task04练习题及答案

在这里插入图片描述
在这里插入图片描述

4.1 union

找出 product 和 product2 中售价高于 500 的商品的基本信息。(表结构如上图所示)

select * from
product where sale_price > 500
union
select * from 
product2 where sale_price > 500;

4.2 对称差

借助对称差的实现方式, 求product和product2的交集。

select * from 
(select * from product 
union 
select * from product2) as p1 
where product_id not in 
(select product_id from product where product_id not in (select product_id from product2) 
union
select product_id from product2 where product_id not in (select product_id from product));

4.3 最高分组

每类商品中售价最高的商品都在哪些商店有售 ?
在这里插入图片描述
在这里插入图片描述

select sp.shop_id,
       sp.shop_name,
       sp.quantity,
       p.product_id,
       p.product_name,
       p.product_type,
       mp.max_price
from product as p
inner join shop_product as sp on p.product_id= sp.product_id
inner join(
select product_type, max(sale_price)  as max_price
from product as p1
group by product_type)  as mp on mp.product_type= p.product_type
and p.sale_price= mp.max_price;

4.4 内连结和关联子查询

分别使用内连结和关联子查询每一类商品中售价最高的商品。

-- 内连接
select p.product_id,
       p.product_name,
       p.product_type,
       mp.max_price,
       p.regist_date
  from product as p
  inner join(
select product_type, max(sale_price)  as max_price
  from product
 group by product_type)  as mp on p.product_type= mp.product_type
   and p.sale_price= mp.max_price;

-- 关联子查询
select p1.product_id,
       p1.product_name,
       p1.product_type,
       p1.sale_price as max_price,
       p1.regist_date
  from product as p1
 where sale_price=(
select max(sale_price)  from product as p2
 where p1.product_type= p2.product_type
 group by product_type) ;

4.5 关联子查询

用关联子查询实现:在product表中,取出 product_id, produc_name, slae_price, 并按照商品的售价从低到高进行排序、对售价进行累计求和。

select p.product_id,
       p.product_name,
       p.product_type,
       p.sale_price,
       (select sum(sale_price) from product as p1 where p.sale_price>= p1.sale_price) as '累计求和'
from product as p
order by sale_price;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值