当前位置:网站首页>数据库系统原理与应用教程(063)—— MySQL 练习题:操作题 39-50(七):SELECT 基本语法联系
数据库系统原理与应用教程(063)—— MySQL 练习题:操作题 39-50(七):SELECT 基本语法联系
2022-07-30 17:26:00 【睿思达DBA_WGX】
数据库系统原理与应用教程(063)—— MySQL 练习题:操作题 39-50(七):SELECT 基本语法联系
39、SQL 基本语法练习(1)
题目:编写 SQL 语句,从 Customers 表中检索所有的 cust_id。
返回结果如下:
| cust_id |
|---|
| A |
| B |
| C |
表结构及数据如下:
/* DROP TABLE IF EXISTS `Customers`; CREATE TABLE IF NOT EXISTS `Customers`( cust_id VARCHAR(255) DEFAULT NULL ); INSERT `Customers` VALUES ('A'),('B'),('C'); */
解答:
mysql> select cust_id from Customers;
+---------+
| cust_id |
+---------+
| A |
| B |
| C |
+---------+
3 rows in set (0.00 sec)
40、SQL 基本语法练习(2)
表 OrderItems 含有非空的列 prod_id 代表商品 id,包含了所有已订购的商品(有些商品已被订购多次)。
| prod_id |
|---|
| a1 |
| a2 |
| a3 |
| a4 |
| a5 |
| a6 |
| a7 |
【问题】编写 SQL 语句,检索并列出所有已订购商品(prod_id)的去重后的清单。
表结构及数据如下:
/* DROP TABLE IF EXISTS `OrderItems`; CREATE TABLE IF NOT EXISTS `OrderItems`( prod_id VARCHAR(255) NOT NULL COMMENT '商品id' ); INSERT `OrderItems` VALUES ('a1'),('a2'),('a3'),('a4'),('a5'),('a6'),('a6'); */
解答:
mysql> select distinct prod_id from OrderItems;
+---------+
| prod_id |
+---------+
| a1 |
| a2 |
| a3 |
| a4 |
| a5 |
| a6 |
+---------+
6 rows in set (0.00 sec)
41、SQL 基本语法练习(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语句,检索所有列。查询结果如下:
| cust_id | cust_name |
|---|---|
| a1 | andy |
| a2 | ben |
| a3 | tony |
| a4 | tom |
| a5 | an |
| a6 | lee |
| a7 | hex |
表结构及数据如下:
/* DROP TABLE IF EXISTS `Customers`; CREATE TABLE IF NOT EXISTS `Customers`( cust_id VARCHAR(255) NOT NULL COMMENT '客户id', cust_name VARCHAR(255) NOT NULL COMMENT '客户姓名' ); INSERT `Customers` VALUES ('a1','andy'),('a2','ben'),('a3','tony'),('a4','tom'),('a5','an'),('a6','lee'),('a7','hex'); */
解答:
mysql> select * from Customers;
+---------+-----------+
| cust_id | cust_name |
+---------+-----------+
| a1 | andy |
| a2 | ben |
| a3 | tony |
| a4 | tom |
| a5 | an |
| a6 | lee |
| a7 | hex |
+---------+-----------+
7 rows in set (0.00 sec)
42、数据排序(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 的顺序显示结果。查询结果如下:
| cust_name |
|---|
| tony |
| tom |
| lee |
| hex |
| ben |
| andy |
| an |
解答:
mysql> select cust_name from Customers order by cust_name desc;
+-----------+
| cust_name |
+-----------+
| tony |
| tom |
| lee |
| hex |
| ben |
| andy |
| an |
+-----------+
7 rows in set (0.00 sec)
43、数据排序(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 对结果进行排序,再按订单日期倒序排列。查询结果如下:
| cust_id | order_num |
|---|---|
| andy | bbbb |
| andy | aaaa |
| bob | cccc |
| dick | dddd |
表结构及数据如下:
/* DROP TABLE IF EXISTS `Orders`; CREATE TABLE IF NOT EXISTS `Orders` ( `cust_id` varchar(255) NOT NULL COMMENT '顾客 ID', `order_num` varchar(255) NOT NULL COMMENT '订单号', `order_date` timestamp NOT NULL COMMENT '订单时间' ); INSERT INTO `Orders` VALUES ('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'); */
解答:
mysql> select cust_id, order_num
-> from Orders order by cust_id, order_date desc;
+---------+-----------+
| cust_id | order_num |
+---------+-----------+
| andy | bbbb |
| andy | aaaa |
| bob | cccc |
| dick | dddd |
+---------+-----------+
4 rows in set (0.00 sec)
44、数据排序(3)
有一个数据表:OrderItems,表中数据如下:
| quantity | item_price |
|---|---|
| 1 | 100 |
| 10 | 1003 |
| 2 | 500 |
【问题】编写 SQL 语句,显示 OrderItems 表中的数量(quantity)和价格(item_price),并按数量由多到少、价格由高到低排序。查询结果如下:
表结构及数据如下:
/* DROP TABLE IF EXISTS `OrderItems`; CREATE TABLE IF NOT EXISTS `OrderItems` ( `quantity` INT(64) NOT NULL COMMENT '数量', `item_price` INT(64) NOT NULL COMMENT '订单价格' ); INSERT INTO `OrderItems` VALUES (1,100), (10,1003), (2,500); */
解答:
/* select quantity, item_price from OrderItems order by quantity desc, item_price desc; */
mysql> select quantity, item_price
-> from OrderItems
-> order by quantity desc, item_price desc;
+----------+------------+
| quantity | item_price |
+----------+------------+
| 10 | 1003 |
| 2 | 500 |
| 1 | 100 |
+----------+------------+
3 rows in set (0.00 sec)
45、使用条件过滤数据(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 |
|---|---|
| a0018 | sockets |
表结构及数据如下:
/* DROP TABLE IF EXISTS `Products`; CREATE TABLE IF NOT EXISTS `Products` ( `prod_id` VARCHAR(255) NOT NULL COMMENT '产品 ID', `prod_name` VARCHAR(255) NOT NULL COMMENT '产品名称', `prod_price` DOUBLE NOT NULL COMMENT '产品价格' ); INSERT INTO `Products` VALUES ('a0018','sockets',9.49), ('a0019','iphone13',600), ('b0019','gucci t-shirts',1000); */
解答:
mysql> select prod_id, prod_name from Products where prod_price = 9.49;
+---------+-----------+
| prod_id | prod_name |
+---------+-----------+
| a0018 | sockets |
+---------+-----------+
1 row in set (0.01 sec)
46、使用条件过滤数据(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 | prod_name |
|---|---|
| a0018 | sockets |
| a0019 | iphone13 |
| b0019 | gucci t-shirts |
解答:
mysql> select prod_id, prod_name from Products where prod_price >= 9;
+---------+----------------+
| prod_id | prod_name |
+---------+----------------+
| a0018 | sockets |
| a0019 | iphone13 |
| b0019 | gucci t-shirts |
+---------+----------------+
3 rows in set (0.00 sec)
47、使用条件过滤数据(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 |
|---|---|
| egg | 3 |
| sockets | 4 |
表结构及数据如下:
/* DROP TABLE IF EXISTS `Products`; CREATE TABLE IF NOT EXISTS `Products` ( `prod_id` VARCHAR(255) NOT NULL COMMENT '产品 ID', `prod_name` VARCHAR(255) NOT NULL COMMENT '产品名称', `prod_price` DOUBLE NOT NULL COMMENT '产品价格' ); INSERT INTO `Products` VALUES ('a0011','egg',3), ('a0019','sockets',4), ('b0019','coffee',15); */
解答:
/* select prod_name, prod_price from Products where prod_price between 3 and 6 order by prod_price; */
mysql> select prod_name, prod_price from Products
-> where prod_price between 3 and 6 order by prod_price;
+-----------+------------+
| prod_name | prod_price |
+-----------+------------+
| egg | 3 |
| sockets | 4 |
+-----------+------------+
2 rows in set (0.00 sec)
48、使用条件过滤数据(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 |
|---|
| a1 |
| a2 |
| a4 |
表结构及数据如下:
/* DROP TABLE IF EXISTS `OrderItems`; CREATE TABLE IF NOT EXISTS `OrderItems`( order_num VARCHAR(255) NOT NULL COMMENT '商品订单号', quantity VARCHAR(255) NOT NULL COMMENT '商品数量' ); INSERT `OrderItems` VALUES ('a1','105'),('a2','1100'),('a2','200'),('a4','1121'),('a5','10'),('a2','19'),('a7','5'); */
解答:
mysql> select distinct order_num from OrderItems where quantity >= 100;
+-----------+
| order_num |
+-----------+
| a1 |
| a2 |
| a4 |
+-----------+
3 rows in set (0.00 sec)
49、使用条件过滤数据(5)
Vendors 表有字段供应商名称(vend_name)、供应商国家(vend_country)、供应商州(vend_state),表中数据如下:
| vend_name | vend_country | vend_state |
|---|---|---|
| apple | USA | CA |
| vivo | CNA | shenzhen |
| huawei | CNA | xian |
【问题】编写 SQL 语句,从 Vendors 表中检索供应商名称(vend_name),仅返回美国加利福尼亚州的供应商(按国家 USA 和州 CA 进行过滤)。查询结果如下:
| vend_name |
|---|
| apple |
表结构及数据如下:
/* DROP TABLE IF EXISTS `Vendors`; CREATE TABLE IF NOT EXISTS `Vendors` ( `vend_name` VARCHAR(255) NOT NULL COMMENT 'vend名称', `vend_country` VARCHAR(255) NOT NULL COMMENT 'vend国家', `vend_state` VARCHAR(255) NOT NULL COMMENT 'vend州' ); INSERT INTO `Vendors` VALUES ('apple','USA','CA'), ('vivo','CNA','shenzhen'), ('huawei','CNA','xian'); */
解答:
mysql> select vend_name from Vendors where vend_country = 'USA' and vend_state = 'CA';
+-----------+
| vend_name |
+-----------+
| apple |
+-----------+
1 row in set (0.00 sec)
50、使用条件过滤数据(6)
OrderItems 表包含了所有已订购的产品(有些产品被订购多次)。
| prod_id | order_num | quantity |
|---|---|---|
| BR01 | a1 | 105 |
| BR02 | a2 | 1100 |
| BR02 | a2 | 200 |
| BR03 | a4 | 1121 |
| BR017 | a5 | 10 |
| BR02 | a2 | 19 |
| BR017 | a7 | 5 |
【问题】编写 SQL 语句,查找所有订购了数量至少100 个的 BR01、BR02 或 BR03 的订单。返回 OrderItems 表的订单号(order_num)、产品 ID(prod_id)和数量(quantity)。查询结果如下:
| order_num | prod_id | quantity |
|---|---|---|
| a1 | BR01 | 105 |
| a2 | BR02 | 1100 |
| a2 | BR02 | 200 |
| a4 | BR03 | 1121 |
表结构及数据如下:
/* DROP TABLE IF EXISTS `OrderItems`; CREATE TABLE IF NOT EXISTS `OrderItems`( prod_id VARCHAR(255) NOT NULL COMMENT '商品号', order_num VARCHAR(255) NOT NULL COMMENT '商品订单号', quantity INT(255) NOT NULL COMMENT '商品数量' ); INSERT `OrderItems` VALUES ('BR01','a1','105'),('BR02','a2','1100'),('BR02','a2','200'),('BR03','a4','1121'),('BR017','a5','10'),('BR02','a2','19'),('BR017','a7','5'); */
解答:
/* select order_num, prod_id, quantity from OrderItems where quantity >= 100 and prod_id in ('BR01', 'BR02', 'BR03'); */
mysql> select order_num, prod_id, quantity from OrderItems
-> where quantity >= 100 and prod_id in ('BR01', 'BR02', 'BR03');
+-----------+---------+----------+
| order_num | prod_id | quantity |
+-----------+---------+----------+
| a1 | BR01 | 105 |
| a2 | BR02 | 1100 |
| a2 | BR02 | 200 |
| a4 | BR03 | 1121 |
+-----------+---------+----------+
4 rows in set (0.01 sec)
边栏推荐
- C# 连接SQL Sever 数据库与数据查询实例 数据仓库
- Insert data into MySQL in C language
- (17)[系统调用]追踪系统调用(0环)
- 952. 按公因数计算最大组件大小 : 枚举质因数 + 并查集运用题
- 一个 15 年 SAP ABAP 开发人员分享的 SAPGUI 一些个性化设置和实用小技巧
- weiit新零售小程序如何探索数字化门店的破局之路
- 你是这样的volatile,出乎意料
- 关于内和调试无法查看ntdll内存的问题
- 京东获取推荐商品列表 API
- Microsoft Office 2019 software download and installation detailed tutorial!
猜你喜欢

LeetCode318:单词长度的最大乘积

每日一题:两数之和

SYSCALL SWAPGS

ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)Solution

SLIM: Sparse Linear Methods (TopN推荐)

Go新项目-编译热加载使用和对比,让开发更自由(3)

Analysis and Simulation of Short Circuit Fault in Power System Based on MATLAB

UE5第一人称射击游戏蓝图教程

代码越写越乱?那是因为你没用责任链

主流的深度学习推理架构有哪些呢?
随机推荐
【综合类型第 34 篇】喜讯!喜讯!!喜讯!!!,我在 CSDN 的第一个实体铭牌
字符串复制、拼接、比较以及分割函数总结(一)
你是这样的volatile,出乎意料
知识蒸馏1:基础原理讲解及yolov5项目实战介绍
Tensorflow模型量化(Quantization)原理及其实现方法
Daily practice------Generate 13-digit bar, Ean-13 code rule: The thirteenth digit is the check code obtained by the calculation of the first twelve digits.
un7.30:Linux——如何在docker容器中显示MySQL的中文字符?
shell快速移植
(18)[系统调用]追踪系统调用(服务表)
实现web实时消息推送的7种方案
SQLServer下载与安装
C陷阱与缺陷 第6章 预处理器 6.4 宏并不是类型定义
leetcode:1488. 避免洪水泛滥【二分 + 贪心】
知识蒸馏4:准备数据集并修改网络配置
[HarekazeCTF2019]Avatar Uploader 1
从零开始的Multi-armed Bandit
哎,这要人老命的缓存一致问题啊
FP6606CMP5 CPC-16L USB类型-C和PD充电控制器 百盛电子代理商
C陷阱与缺陷 第7章 可移植性缺陷 7.3 整数的大小
Mathematical Principles of Graph Convolutional Neural Networks——A Preliminary Study on Spectral Graph Theory and Fourier Transform