当前位置:网站首页>牛客Mysql——SQL必知必会
牛客Mysql——SQL必知必会
2022-07-28 05:32:00 【墨客小书虫】
前言
作者简介:友友们大家好,我是你们的墨客小书虫
个人主页:墨客小书虫
系列专栏:剑指offer
觉得小墨客写的不错的话 麻烦动动小手 点赞 收藏 评论,感谢大家了
今天给大家带来的系列是:MYsql——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 OrderItems
第3题
现在有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
排序检索数据
第1题
有表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
第2题
有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
第3题
假设有一个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
第4题
有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;
过滤数据
第1题
有表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
第2题
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
第3题
有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
第4题
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
一起加油
边栏推荐
猜你喜欢

Remotely access the local website of services such as neo4j on the ECS

Gd32f407 porting freertos+lwip

教程篇(7.0) 06. 零信任网络访问ZTNA * FortiClient EMS * Fortinet 网络安全专家 NSE 5

Redis哨兵模式及集群

Serial port configuration of raspberry pie

A timed task reminder tool

CAS vs 数据库乐观锁

低端电脑如何深度学习秘籍-使用mistGPU计算平台

VNC Timed out waiting for a response from the computer

删除链表中的节点——每日一题
随机推荐
Database-Trivial
Blueberry pie Bluetooth debugging process
Redis哨兵模式及集群
Sysevr environment configuration: joern-0.3.1, neo4j-2.1.5, py2neo2.0
guava之EventBus
小红花STL
Log in to Oracle10g OEM and want to manage the monitor program, but the account password input page always pops up
低端电脑如何深度学习秘籍-使用mistGPU计算平台
guava之限流RateLimiter
Gd32f407 porting freertos+lwip
N天前的日期
Review of C language (variable parameters)
Serial port configuration of raspberry pie
How did tensor leak your memory / video memory
MHA高可用配置及故障切换
MySQL排除节假日,计算日期差
easypoi导出表格带echars图表
shell---循环语句练习
Insert sort of sort
高响应比优先