每日一SQL 【销售分析 III】

文章目录

问题

案例

执行顺序

SQL 语句的执行顺序(核心步骤)

同一层级的select查询内部, 别名在整个 SELECT 计算完成前不生效

使用分组解决

sql 复制代码
select distinct s.product_id, Product.product_name from Sales s
                  left join Product on s.product_id = Product.product_id
where s.product_id not in
(select product_id from Sales where sale_date not between '2019-01-01' and '2019-03-31');

解释

属于区间['2019-01-01', '2019-03-31']

等价于不属于 (负无穷, '2019-01-01') ∪ ( '2019-03-31', 正无穷)

写成代码: product_id not in (... not between '2019-01-01' and '2019-03-31')