💟💟前言
🥇作者简介:友友们大家好,我是你们的墨客小书虫😗😗
🥈个人主页:墨客小书虫🚗
🥉 系列专栏:剑指offer
觉得小墨客写的不错的话 麻烦动动小手 点赞👍 收藏⭐ 评论📄,感谢大家了
今天给大家带来的系列是:MYsql——SQL必知必会
现有表Customers如下:
| cust_id |
|---|
| A |
| B |
| C |
【问题】
编写 SQL 语句,从 Customers 表中检索所有的cust_id
select cust_id from Customers
表OrderItems含有非空的列prod_id代表商品id,包含了所有已订购的商品(有些已被订购多次)。
| prod_id |
|---|
| a1 |
| a2 |
| a3 |
| a4 |
| a5 |
| a6 |
| a7 |
【问题】编写SQL 语句,检索并列出所有已订购商品(prod_id)的去重后的清单。
select distinct prod_id from OrderItems
现在有Customers 表(表中含有列cust_id代表客户id,cust_name代表客户姓名)
| cust_id | cust_name |
|---|---|
| a1 | andy |
| a2 | ben |
| a3 | tony |
| a4 | tom |
| a5 | an |
| a6 | lee |
| a7 | hex |
【问题】需要编写 SQL语句,检索所有列。
select cust_id,cust_name from Customers
有表Customers,cust_id代表客户id,cust_name代表客户姓名。
| cust_id | cust_name |
|---|---|
| a1 | andy |
| a2 | ben |
| a3 | tony |
| a4 | tom |
| a5 | an |
| a6 | lee |
| a7 | hex |
【问题】从 Customers 中检索所有的顾客名称(cust_name),并按从 Z 到 A 的顺序显示结果。
select cust_name from Customers order by cust_name desc
有Orders表
| cust_id | order_num | order_date |
|---|---|---|
| andy | aaaa | 2021-01-01 00:00:00 |
| andy | bbbb | 2021-01-01 12:00:00 |
| bob | cccc | 2021-01-10 12:00:00 |
| dick | dddd | 2021-01-11 00:00:00 |
【问题】编写 SQL 语句,从 Orders 表中检索顾客 ID(cust_id)和订单号(order_num),并先按顾客 ID 对结果进行排序,再按订单日期倒序排列。
select
cust_id,order_num
from Orders
order by cust_id asc,order_date desc
假设有一个OrderItems表
| quantity | item_price |
|---|---|
| 1 | 100 |
| 10 | 1003 |
| 2 | 500 |
【问题】
编写 SQL 语句,显示 OrderItems 表中的数量(quantity)和价格(item_price),并按数量由多到少、价格由高到低排序。
select quantity,item_price from OrderItems order by quantity desc,item_price desc
有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;
有表Products
| prod_id | prod_name | prod_price |
|---|---|---|
| a0018 | sockets | 9.49 |
| a0019 | iphone13 | 600 |
| b0018 | gucci t-shirts | 1000 |
【问题】从 Products 表中检索产品 ID(prod_id)和产品名称(prod_name),只返回价格为 9.49 美元的产品。
【示例结果】返回prod_id和prod_name
select prod_id,prod_name from Products where prod_price=9.49
Products 表
| prod_id | prod_name | prod_price |
|---|---|---|
| a0018 | sockets | 9.49 |
| a0019 | iphone13 | 600 |
| b0019 | gucci t-shirts | 1000 |
【问题】编写 SQL 语句,从 Products 表中检索产品 ID(prod_id)和产品名称(prod_name),只返回价格为 9 美元或更高的产品。
【示例答案】返回prod_id商品id和prod_name商品名称
select prod_id,prod_name from Products where prod_price>=9
有Products 表
| prod_id | prod_name | prod_price |
|---|---|---|
| a0011 | egg | 3 |
| a0019 | sockets | 4 |
| b0019 | coffee | 15 |
【问题】编写 SQL 语句,返回 Products 表中所有价格在 3 美元到 6 美元之间的产品的名称(prod_name)和价格(prod_price),然后按价格对结果进行排序
【示例结果】返回商品名称prod_name和商品价格prod_price
select
prod_name,
prod_price
from Products
where prod_price between 3 and 6
order by prod_price asc
OrderItems表含有:订单号order_num,quantity产品数量
| order_num | quantity |
|---|---|
| a1 | 105 |
| a2 | 1100 |
| a2 | 200 |
| a4 | 1121 |
| a5 | 10 |
| a2 | 19 |
| a7 | 5 |
【问题】从 OrderItems 表中检索出所有不同且不重复的订单号(order_num),其中每个订单都要包含 100 个或更多的产品。
【示例结果】返回订单号列order_num
select
distinct order_num
from OrderItems
where quantity>=100
一起加油