当前位置:网站首页>SQL编程task03作业-复杂一点的查询
SQL编程task03作业-复杂一点的查询
2022-06-22 09:19:00 【浅浅望】
文章目录
1 学习内容
MySQL-learning03 复杂一点的查询
DataWhale SQL组队学习
2 练习题-第一部分
product表格:
2.1 创建视图
创建出满足下述三个条件的视图(视图名称为 ViewPractice5_1)。使用product(商品)表作为参照表,假设表中包含初始状态的8 行数据。
条件 1: 销售单价大于等于 1000 日元。
条件 2: 登记日期是 2009 年 9 月 20 日。
条件 3: 包含商品名称、销售单价和登记日期三列。

SQL代码:
create view ViewPractices5_1 (product_name,sale_price,regist_date)
as
select product_name,sale_price,regist_date
from product
where sale_price >= 1000
and regist_date = '2009-9-20';
2.2 给视图插入数据
向习题一中创建的视图 ViewPractice5_1 中插入如下数据,会得到什么样的结果呢?
INSERT INTO ViewPractice5_1 VALUES (' 刀子 ', 300, '2009-11-02');
结果产生错误,没有默认值
我的猜想: 视图是原始数据的一个窗口,而我们通过这个视图使用INSERT对原始数据添加记录时,无法对原表的product_id(NOT NULL)和product_type(NOT NULL)进行赋值操作,因此不能这样使用。
实际原因: 能力有限,尚不清楚
2.3 使用标量子查询
请根据如下结果编写 SELECT 语句,其中 sale_price_all 列为全部商品的平均销售单价。

SQL代码:
SELECT product_id,
product_name,
product_type,
sale_price,
(SELECT AVG(sale_price)
FROM product) AS sale_price_all
FROM product;
2.4 使用关联子查询
请根据习题一中的条件编写一条 SQL 语句,创建一幅包含如下数据的视图(名称为AvgPriceByType)。
create view AvgPriceByType
as
select product_id,
product_name,
p1.product_type,
sale_price,
p2.avg_sale_price
from product p1,
(select product_type,avg(sale_price) as avg_sale_price
from product
group by product_type
) p2
where p1.product_type = p2.product_type;
3 练习题-第二部分
3.1 含有NULL的运算
运算或者函数中含有 NULL 时,结果全都会变为NULL ?(判断题)
错误,如count(null)函数查询时结果恒为零。
3.2 IN语句
对 product(商品)表执行如下 2 条 SELECT 语句,能够得到什么样的结果呢?
SQL语句1:
SELECT product_name, purchase_price
FROM product
WHERE purchase_price NOT IN (500, 2800, 5000);
查询结果:
SQL语句2:
SELECT product_name, purchase_price
FROM product
WHERE purchase_price NOT IN (500, 2800, 5000, NULL);
查询结果:
原因:
这是因为所有的值都不等于null,任何IN或者NOT IN(NULL)均为空值。
3.3 CASE+IN语句
按照销售单价( sale_price)对练习 3.2 中的 product(商品)表中的商品进行如下分类。
低档商品: 销售单价在1000日元以下**(T恤衫、办公用品、叉子、擦菜板、 圆珠笔)**
中档商品: 销售单价在1001日元以上3000日元以下**(菜刀**)
高档商品:销售单价在3001日元以上(运动T恤、高压锅)
请编写出统计上述商品种类中所包含的商品数量的 SELECT 语句,结果如下所示。
结果如下:
 and sale_price <= 1000 then 1 else 0 end) as low_price,
sum(case when product_name in ('菜刀') and sale_price between 1001 and 3000 then 1 else 0 end) as mid_price,
sum(case when product_name in('运动T恤','高压锅')and sale_price >3000 then 1 else 0 end) as high_price
from product;
边栏推荐
猜你喜欢

機器學習|nltk_Data下載錯誤|nltk的stopwords語料下載錯誤解决方法

User insight into the video industry in January 2022: active users began to pick up under the influence of holidays

值(址)传递,看清名字,别掉沟里

A readme of an old open source person - how to do open source well

Project optimization + online (Master?)

Why use gradient descent method
![[network security officer] an attack technology that needs to be understood - high hidden and high persistent threats](/img/c9/c0ee95e816cac698f5397cc369d9ec.jpg)
[network security officer] an attack technology that needs to be understood - high hidden and high persistent threats

Overview of microservice architecture

Volumedetect of ffmpeg

Feedforward and backpropagation
随机推荐
[ZOJ] P3228 Searching the String
Shengdun technology joined dragon lizard community to build a new open source ecosystem
container_ of
在ensp上做多出口服務器映射
list_ head
Cocoscreator compilation error record could not write cache value gradle\daemon\4.10.3\regi
container_of
mknod
微服务架构概述
Langchao state-owned assets cloud: state-owned assets as a guide to help state-owned enterprises use data to enrich their wisdom in the cloud
进程状态汇总
[学习笔记] 回滚莫队
cocoscreator编译报错记录 Could not write cache value 。。。.gradle\daemon\4.10.3\regi
Opencv daily function histogram correlation (3)
A readme of an old open source person - how to do open source well
Servlet的生命周期
Try/finally --return those things
[node] node+ SMS API to realize verification code login
DHCP中继代理
重写?重载?你晕了吗?