练习题-第一部分
3.1创建出满足下述三个条件的视图(视图名称为 ViewPractice5_1)。使用 product(商品)表作为参照表,假设表中包含初始状态的 8 行数据。
- 条件 1:销售单价大于等于 1000 日元。
- 条件 2:登记日期是 2009 年 9 月 20 日。
- 条件 3:包含商品名称、销售单价和登记日期三列。
CREATE VIEW ViewPractice5_1 (product_type, sale_price,regist_date)
AS
SELECT product_type,sale_price,regist_date
FROM product
where sale_price>=1000
and regist_date="2009-09-20";
3.2
向习题一中创建的视图 ViewPractice5_1
中插入如下数据,会得到什么样的结果?为什么?
输出的结果保持和习题一的结果没有区别,因为插入的数据不满足视图 ViewPractice5_1的条件。
3.3
请根据如下结果编写 SELECT 语句,其中 sale_price_avg 列为全部商品的平均销售单价。
select product_id ,product_name ,product_type ,sale_price ,
(SELECT AVG(sale_price)
from product ) as sale_price_avg
from product
3.4
请根据习题一中的条件编写一条 SQL 语句,创建一幅包含如下数据的视图(名称为AvgPriceByType)。
CREATE VIEW AvgPriceByType (product_id,product_name ,product_type ,sale_price ,sale_price_avg_type)
AS
select product_id,product_name ,product_type ,sale_price ,
(SELECT AVG(sale_price)
FROM product AS p2
WHERE p1.product_type = p2.product_type
GROUP BY product_type) as sale_price_avg_type
from product as p1 ;
练习题-第二部分
3.5
运算中含有 NULL 时,运算结果是否必然会变为NULL ?
是
3.6
对本章中使用的 product
(商品)表执行如下 2 条 SELECT
语句,能够得到什么样的结果呢?
(1)
选出商品成本价格不为500, 2800, 5000的商品
(2)
因为in 无法选出null的数据
3.7
按照销售单价( sale_price
)对练习 3.6 中的 product
(商品)表中的商品进行如下分类。
- 低档商品:销售单价在1000日元以下(T恤衫、办公用品、叉子、擦菜板、 圆珠笔)
- 中档商品:销售单价在1001日元以上3000日元以下(菜刀)
- 高档商品:销售单价在3001日元以上(运动T恤、高压锅)
SELECT sum(case when sale_price<= 1000 then 1 else 0 end) as low_price,
sum(case when sale_price between 1001 and 3000 then 1 else 0 end) as mid_price,
sum(case when sale_price> 3000 then 1 else 0 end) as high_price
FROM product;