SQL语句每日一练一

1.现有表 Customers 如下:
cust_id
A
B
C

编写 SQL 语句,从 Customers 表中检索所有的 cust_id

select cust_id from Customers;
2.表 OrderItems 含有非空的列 prod_id 代表商品 id,包含了所有已订购的商品(有些已被订购多次)。
prod_id
a1
a2
a3
a4
a5
a6
a7

编写 SQL 语句,检索并列出所有已订购商品(prod_id)的去重后的清单。

select distinct prod_id from  OderItems;
3.现在有 Customers 表(表中含有列 cust_id 代表客户 id,cust_name 代表客户姓名)
cust_idcust_name
a1andy
a2ben
a3tony
a4tom
a5an
a6lee
a7hex

需要编写 SQL 语句,检索所有列。

select cust_id,cust_name from Customers;
4.有表 Customerscust_id 代表客户 id,cust_name 代表客户姓名。
cust_idcust_name
a1andy
a2ben
a3tony
a4tom
a5an
a6lee
a7hex

Customers 中检索所有的顾客名称(cust_name),并按从 Z 到 A 的顺序显示结果。

select cust_name from Customers order by cust_name desc;
5.有 Orders 表:
cust_idorder_numorder_date
andyaaaa2021-01-01 00:00:00
andybbbb2021-01-01 12:00:00
bobcccc2021-01-10 12:00:00
dickdddd2021-01-11 00:00:00

编写 SQL 语句,从 Orders 表中检索顾客 ID(cust_id)和订单号(order_num),并先按顾客 ID 对结果进行排序,再按订单日期倒序排列。

select cust_id,order_id from Customers order by cust_id asc,order_date desc;
6.假设有一个 OrderItems 表:
quantityitem_price
1100
101003
2500

编写 SQL 语句,显示 OrderItems 表中的数量(quantity)和价格(item_price),并按数量由多到少、价格由高到低排序。

select quantity,item_price from OrderItems order by quantity desc,item_price desc; 
7.有 Vendors 表:
vend_name
海底捞
小龙坎
大龙燚

下面的 SQL 语句有问题吗?尝试将它改正确,使之能够正确运行,并且返回结果根据vend_name 逆序排列。

SELECT vend_name,
FROM Vendors
ORDER vend_name DESC
select vend_name from Vendors order by vend_name desc;
8.有表 Products
prod_idprod_nameprod_price
a0018sockets9.49
a0019iphone13600
b0018gucci t-shirts1000

【问题】从 Products 表中检索产品 ID(prod_id)和产品名称(prod_name),只返回价格为 9.49 美元的产品。

select prod_id,prod_name from Products where prod_price = 9.49;

【问题】编写 SQL 语句,从 Products 表中检索产品 ID(prod_id)和产品名称(prod_name),只返回价格为 9 美元或更高的产品。

select prod_id,prod_name from Products where prod_price >= 9;
9.有表 Products
prod_idprod_nameprod_price
a0011egg3
a0019sockets4
b0019coffee15

【问题】编写 SQL 语句,返回 Products 表中所有价格在 3 美元到 6 美元之间的产品的名称(prod_name)和价格(prod_price),然后按价格对结果进行排序。

select prod_id,prod_name from Products where prod_price between 3 and 6 order by prod_price asc;
select prod_id,prod_name from Products where prod_price >=3 and prod_price <=6 order by prod_price desc;
10.OrderItems 表含有:订单号 order_numquantity产品数量
order_numquantity
a1105
a21100
a2200
a41121
a510
a219
a75

【问题】从 OrderItems 表中检索出所有不同且不重复的订单号(order_num),其中每个订单都要包含 100 个或更多的产品

select order_num from OrderItems group by order_num having sum(quantity) >= 100;
11.Vendors 表有字段供应商名称(vend_name)、供应商国家(vend_country)、供应商州(vend_state
vend_namevend_countryvend_state
appleUSACA
vivoCNAshenzhen
huaweiCNAxian

【问题】编写 SQL 语句,从 Vendors 表中检索供应商名称(vend_name),仅返回加利福尼亚州的供应商(这需要按国家[USA]和州[CA]进行过滤,没准其他国家也存在一个 CA)

select vend_name from Vendors where vend_country = 'USA' and vebd_state = 'CA';
12.OrderItems 表包含了所有已订购的产品(有些已被订购多次)。
prod_idorder_numquantity
BR01a1105
BR02a21100
BR02a2200
BR03a41121
BR017a510
BR02a219
BR017a75

【问题】编写 SQL 语句,查找所有订购了数量至少 100 个的 BR01BR02BR03 的订单。你需要返回 OrderItems 表的订单号(order_num)、产品 ID(prod_id)和数量(quantity),并按产品 ID 和数量进行过滤。

select prod_id,order_num,quantity from OrderItems where prod_id in(BRO1,BR02,BR03) and quantity >= 100; 
13.有表 Products
prod_idprod_nameprod_price
a0011egg3
a0019sockets4
b0019coffee15

【问题】编写 SQL 语句,返回所有价格在 3 美元到 6 美元之间的产品的名称(prod_name)和价格(prod_price),使用 AND 操作符,然后按价格对结果进行升序排序。

select prod_name,prod_price from Products where prod_price>=3 and pord_price<=6 order by prod_price asc;
14.供应商表 Vendors 有字段供应商名称 vend_name、供应商国家 vend_country、供应商省份 vend_state
vend_namevend_countryvend_state
appleUSACA
vivoCNAshenzhen
huaweiCNAxian

【问题】修改正确下面 sql,使之正确返回。

SELECT vend_name
FROM Vendors
ORDER BY vend_name
WHERE vend_country = 'USA' AND vend_state = 'CA';
select vend_name from Vendors where vend_country = 'USA' and vend_state = 'CA' order by vend_name;
15.Products 表如下:
prod_nameprod_desc
a0011usb
a0019iphone13
b0019gucci t-shirts
c0019gucci toy
d0019lego toy

【问题】编写 SQL 语句,从 Products 表中检索产品名称(prod_name)和描述(prod_desc),仅返回描述中包含 toy 一词的产品名称。

select prod_name,prod_desc from Porducts where prod_desc like '%toy%'; 
16.Products 表如下:
prod_nameprod_desc
a0011usb
a0019iphone13
b0019gucci t-shirts
c0019gucci toy
d0019lego toy

【问题】编写 SQL 语句,从 Products 表中检索产品名称(prod_name)和描述(prod_desc),仅返回描述中未出现 toy 一词的产品,最后按”产品名称“对结果进行排序。

select prod_name,prod_desc from Products where prod_desc not like '%toy%' order by prod_name;
17.Products 表如下:
prod_nameprod_desc
a0011usb
a0019iphone13
b0019gucci t-shirts
c0019gucci toy
d0019lego carrots toy

【问题】编写 SQL 语句,从 Products 表中检索产品名称(prod_name)和描述(prod_desc),仅返回描述中同时出现 toycarrots 的产品。有好几种方法可以执行此操作,但对于这个挑战题,请使用 AND 和两个 LIKE 比较。

select prod_name,prod_desc from Products where prod_desc like '%toy%' and prod_desc like '%carrots%';
18.Products 表如下:
prod_nameprod_desc
a0011usb
a0019iphone13
b0019gucci t-shirts
c0019gucci toy
d0019lego toy carrots

【问题】编写 SQL 语句,从 Products 表中检索产品名称(prod_name)和描述(prod_desc),仅返回在描述中以先后顺序同时出现 toy 和 carrots 的产品。提示:只需要用带有三个 % 符号的 LIKE 即可。

select prod_name,prod_desc where prod_desc like '%toy%carrots%';
19.有表 Vendors 代表供应商信息,vend_id 供应商 id、vend_name 供应商名称、vend_address 供应商地址、vend_city 供应商城市。
vend_idvend_namevend_addressvend_city
a001tencent cloudaddress1shenzhen
a002huawei cloudaddress2dongguan
a003aliyun cloudaddress3hangzhou
a003netease cloudaddress4guangzhou

【问题】编写 SQL 语句,从 Vendors 表中检索 vend_idvend_namevend_addressvend_city,将 vend_name 重命名为 vname,将 vend_city 重命名为 vcity,将 vend_address 重命名为 vaddress,按供应商名称对结果进行升序排序。

select vend_id,vend_name vname,vend_address vaddress,vend_city vcity from Vendors order by vname;
select vend_id,vend_name as vname,vend_address as vaddress,vend_city as vcity from Vendors order by vname;
20.【问题】编写 SQL 语句,从 Products 表中返回 prod_idprod_pricesale_pricesale_price 是一个包含促销价格的计算字段。提示:可以乘以 0.9,得到原价的 90%(即 10%的折扣)。
select prid_id,prod_price,prod_price*0.9 as sale_price from Prodects;
  • 8
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值