mami mami
Home
知识体系总览
  • 数据结构与算法
  • 计算机网络
  • 操作系统
  • MySQL
  • Java 基础
  • Java 集合
  • Java 并发
  • Java 虚拟机
  • Spring
  • Redis
  • 唠唠嗑儿
  • 读书笔记
归档
GitHub (opens new window)
Home
知识体系总览
  • 数据结构与算法
  • 计算机网络
  • 操作系统
  • MySQL
  • Java 基础
  • Java 集合
  • Java 并发
  • Java 虚拟机
  • Spring
  • Redis
  • 唠唠嗑儿
  • 读书笔记
归档
GitHub (opens new window)
  • 小牛肉的 MySQL 知识体系结构
  • SQL 刷题

    • 牛客题霸:必知必会
      • 检索数据
        • SQL1 从 Customers 表中检索所有的 ID
        • SQL2 检索并列出已订购产品的清单
        • SQL3 检索所有列
      • 排序检索数据
        • SQL4 检索顾客名称并且排序
        • SQL5 对顾客 ID 和日期排序
        • SQL6 按照数量和价格排序
        • SQL7 检查 SQL 语句
      • 过滤数据
        • SQL8 返回固定价格的产品
        • SQL9 返回更高价格的产品
        • SQL10 返回产品并且按照价格排序
        • SQL11 返回更多的产品
      • 高级数据过滤
      • 用通配符进行过滤
      • 创建计算字段
      • 使用函数处理数据
      • 汇总数据
      • 分组数据
      • 使用子查询
      • 联结表
      • 创建高级联结
      • 组合查询
    • 牛客题霸:进阶
    • 牛客题霸:大厂面试真题
  • MySQL 体系架构

  • InnoDB 存储引擎

  • 索引

  • 事务

  • SQL 优化

  • MySQL
  • SQL 刷题
itslae
2022-04-01
目录

牛客题霸:必知必会

题目来源:

  • 牛客题霸 - SQL 必知必会 (opens new window)

# 检索数据

# SQL1 从 Customers 表中检索所有的 ID

现有表Customers如下:

cust_id
A
B
C

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

select cust_id 
from Customers;

# SQL2 检索并列出已订购产品的清单

表OrderItems含有非空的列prod_id代表商品id,包含了所有已订购的商品(有些已被订购多次)。

prod_id
a1
a2
a3
a4
a5
a6
a7

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

select distinct prod_id 
from OrderItems;

# SQL3 检索所有列

现在有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;

# 排序检索数据

# SQL4 检索顾客名称并且排序

有表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

# 根据列索引位置(索引下标从 1 开始)
select cust_name 
from Customers 
order by 1 desc

# SQL5 对顾客 ID 和日期排序

有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, order_date desc;

# 根据列索引位置
select cust_id, order_num 
from Orders 
order by 0, 2 desc;

# SQL6 按照数量和价格排序

假设有一个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;

# 根据列索引位置
select quantity, item_price
from OrderItems
order by 1 desc, 2 desc;

# SQL7 检查 SQL 语句

有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;

# 过滤数据

# SQL8 返回固定价格的产品

有表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 美元的产品。

select prod_id, prod_name
from Products
where prod_price = 9.49;

# SQL9 返回更高价格的产品

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 美元或更高的产品。

select prod_id, prod_name
from Products
where prod_price >= 9;

# SQL10 返回产品并且按照价格排序

有Products 表

prod_id prod_name prod_price
a0011 egg 3
a0019 sockets 4
b0019 coffee 15

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

select prod_name, prod_price
from Products
where prod_price between 3 and 6
order by prod_price;

# 或者
select prod_name, prod_price
from Products
where prod_price >= 3 and prod_price <= 6
order by prod_price;

# SQL11 返回更多的产品

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 个或更多的产品。

select distinct order_num
from OrderItems
where quantity >= 100;

# 高级数据过滤

# 用通配符进行过滤

# 创建计算字段

# 使用函数处理数据

# 汇总数据

# 分组数据

# 使用子查询

# 联结表

# 创建高级联结

# 组合查询

上次更新: 2022/06/13, 02:15:48
小牛肉的 MySQL 知识体系结构
牛客题霸:进阶

← 小牛肉的 MySQL 知识体系结构 牛客题霸:进阶→

最近更新
01
器件课程
06-13
02
上传github
06-13
03
研究生
06-03
更多文章>
Theme by Vdoing | Copyright © 2019-2022 itslae| 心之所愿,素履以往
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式