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)