SQL语句每日一练四

1.Customers表代表顾客信息含有顾客 idcust_id和 顾客名称cust_name
cust_idcust_name
cust10andy
cust1ben
cust2tony
cust22tom
cust221an
cust2217hex

Orders表代表订单信息含有订单号order_num和顾客 idcust_id

order_numcust_id
a1cust10
a2cust1
a3cust2
a4cust22
a5cust221
a7cust2217

【问题】使用 INNER JOIN 编写 SQL 语句,检索每个顾客的名称(Customers 表中的 cust_name)和所有的订单号(Orders 表中的 order_num),最后根据顾客姓名 cust_name 升序返回。

select t1.cust_name,t2.order_num from Customers t1
					inner join Orders t2 using(cust_id)
order by t1.cust_name;
2.Orders表代表订单信息含有订单号order_num和顾客 idcust_id
order_numcust_id
a1cust10
a2cust1
a3cust2
a4cust22
a5cust221
a7cust2217

Customers表代表顾客信息含有顾客 idcust_id和 顾客名称cust_name

cust_idcust_name
cust10andy
cust1ben
cust2tony
cust22tom
cust221an
cust2217hex
cust40ace

【问题】检索每个顾客的名称(Customers 表中的 cust_name)和所有的订单号(Orders 表中的 order_num),列出所有的顾客,即使他们没有下过订单。最后根据顾客姓名 cust_name 升序返回。

select cust_name,order_num from Customer
							left join Orders using(cust_id)
order by cust_name;
3.Products 表为产品信息表含有字段 prod_id 产品 id、prod_name 产品名称
prod_idprod_name
a0001egg
a0002sockets
a0013coffee
a0003cola
a0023soda

OrderItems表为订单信息表含有字段order_num订单号和产品 idprod_id

prod_idorder_num
a0001a105
a0002a1100
a0002a200
a0013a1121
a0003a10
a0003a19
a0003a5

【问题】使用外连接(left join、 right join、full join)联结 Products 表和 OrderItems 表,返回产品名称(prod_name)和与之相关的订单号(order_num)的列表,并按照产品名称升序排序。

select prod_name,order_num from Products 
left join OrderItems using(prod_id)
order by prod_name;
4.Products 表为产品信息表含有字段 prod_id 产品 id、prod_name 产品名称
prod_idprod_name
a0001egg
a0002sockets
a0013coffee
a0003cola
a0023soda

OrderItems表为订单信息表含有字段order_num订单号和产品 idprod_id

prod_idorder_num
a0001a105
a0002a1100
a0002a200
a0013a1121
a0003a10
a0003a19
a0003a5

【问题】

使用 OUTER JOIN 联结 Products 表和 OrderItems 表,返回产品名称(prod_name)和每一项产品的总订单数(不是订单号),并按产品名称升序排序。

select prod_name,count(order_num) as total from Products 
left join OrderItesm using(prod_id)
group by prod_name
order by prod_name;
5.有 Vendors 表含有 vend_id (供应商 id)
vend_id
a0002
a0013
a0003
a0010

Products 表含有 vend_id(供应商 id)和 prod_id(供应产品 id)

vend_idprod_id
a0001egg
a0002prod_id_iphone
a00113prod_id_tea
a0003prod_id_vivo phone
a0010prod_id_huawei phone

【问题】列出供应商(Vendors 表中的 vend_id)及其可供产品的数量,包括没有产品的供应商。你需要使用 OUTER JOIN 和 COUNT()聚合函数来计算 Products 表中每种产品的数量,最后根据 vend_id 升序排序。

select vend_id,count(prod_id) as total from Vendors 
left join Products using(vend_id)
group by vend_id
order by vend_id;
6.表 OrderItems 包含订单产品信息,字段 prod_id 代表产品 id、quantity 代表产品数量
prod_idquantity
a0001105
a0002100
a0002200
a00131121
a000310
a000319
a00035
BNBG10002

【问题】将两个 SELECT 语句结合起来,以便从 OrderItems 表中检索产品 id(prod_id)和 quantity。其中,一个 SELECT 语句过滤数量为 100 的行,另一个 SELECT 语句过滤 id 以 BNBG 开头的产品,最后按产品 id 对结果进行升序排序。

select prod_id,quantity from OrderItems where quantity = 100
union 
select prod_id,quantity from OrderItems where prod_id = 'BEGIN%';
7.表 OrderItems 包含订单产品信息,字段 prod_id 代表产品 id、quantity 代表产品数量。
prod_idquantity
a0001105
a0002100
a0002200
a00131121
a000310
a000319
a00035
BNBG10002

【问题】将两个 SELECT 语句结合起来,以便从 OrderItems 表中检索产品 id(prod_id)和 quantity。其中,一个 SELECT 语句过滤数量为 100 的行,另一个 SELECT 语句过滤 id 以 BNBG 开头的产品,最后按产品 id 对结果进行升序排序。 注意:这次仅使用单个 SELECT 语句。

select prod_id,quantity from OrderItems where prod_id like 'BEGIN%' or quantity = 100;   
8.Products 表含有字段 prod_name 代表产品名称
prod_name
flower
rice
ring
umbrella

Customers 表代表顾客信息,cust_name 代表顾客名称

cust_name
andy
ben
tony
tom
an
lee
hex

【问题】编写 SQL 语句,组合 Products 表中的产品名称(prod_name)和 Customers 表中的顾客名称(cust_name)并返回,然后按产品名称对结果进行升序排序。

# UNION 结果集中的列名总是等于 UNION 中第一个 SELECT 语句中的列名
select prod_name from Products 
union
select cust_name from Customers
order by prod_name;
9.表 Customers 含有字段 cust_name 顾客名、cust_contact 顾客联系方式、cust_state 顾客州、cust_email 顾客 email
cust_namecust_contactcust_statecust_email
cust108695192MIcust10@cust.com
cust18695193MIcust1@cust.com
cust28695194ILcust2@cust.com

【问题】修正下面错误的 SQL

SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust_state = 'MI'
ORDER BY cust_name;
UNION
SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust_state = 'IL'ORDER BY cust_name;
select cust_name,cust_contact,cust_email from Customers where cust_state = 'MI'
union
select cust_name,cust_contact,cust_email from Customers where cust_state = 'IL'
order by cust_name;

select cust_name,cust_contact,cust_name from Customers where cust_state = 'MI' or cust_state = 'IL' order by cust_name; 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值