• 数据库系统原理与应用教程(063)—— MySQL 练习题:操作题 39-50(七)


    数据库系统原理与应用教程(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');
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    解答:

    mysql> select cust_id from Customers;
    +---------+
    | cust_id |
    +---------+
    | A       |
    | B       |
    | C       |
    +---------+
    3 rows in set (0.00 sec)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    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');
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    解答:

    mysql> select distinct prod_id from OrderItems;
    +---------+
    | prod_id |
    +---------+
    | a1      |
    | a2      |
    | a3      |
    | a4      |
    | a5      |
    | a6      |
    +---------+
    6 rows in set (0.00 sec)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    41、SQL 基本语法练习(3)

    有 Customers 表(表中含有列 cust_id 代表客户 id,cust_name 代表客户姓名),数据如下:

    cust_idcust_name
    a1andy
    a2ben
    a3tony
    a4tom
    a5an
    a6lee
    a7hex

    【问题】编写 SQL语句,检索所有列。查询结果如下:

    cust_idcust_name
    a1andy
    a2ben
    a3tony
    a4tom
    a5an
    a6lee
    a7hex

    表结构及数据如下:

    /*
    
    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');
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    解答:

    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    42、数据排序(1)

    有数据表 Customers,其中 cust_id 代表客户 id,cust_name 代表客户姓名。

    cust_idcust_name
    a1andy
    a2ben
    a3tony
    a4tom
    a5an
    a6lee
    a7hex

    【问题】从 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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    43、数据排序(2)

    有数据表:Orders,表中数据如下:

    cust_idorder_numorder_date
    andyaaaa2021-01-01 00:00:00
    andybbbb2021-01-01 12:00:00
    bobcccc2021-01-10 12:00:00
    dickdddd2021-01-11 00:00:00

    【问题】编写 SQL 语句,从 Orders 表中检索顾客 ID(cust_id)和订单号(order_num),并先按顾客 ID 对结果进行排序,再按订单日期倒序排列。查询结果如下:

    cust_idorder_num
    andybbbb
    andyaaaa
    bobcccc
    dickdddd

    表结构及数据如下:

    /*
    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');
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    解答:

    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    44、数据排序(3)

    有一个数据表:OrderItems,表中数据如下:

    quantityitem_price
    1100
    101003
    2500

    【问题】编写 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);
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    解答:

    /*
    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    45、使用条件过滤数据(1)

    有数据表:Products,表中数据如下:

    prod_idprod_nameprod_price
    a0018sockets9.49
    a0019iphone13600
    b0018gucci t-shirts1000

    【问题】从 Products 表中检索产品 ID(prod_id)和产品名称(prod_name),只返回价格为 9.49 元的产品。查询结果如下:

    prod_idprod_name
    a0018sockets

    表结构及数据如下:

    /*
    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);
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    解答:

    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    46、使用条件过滤数据(2)

    有数据表:Products,表中数据如下:

    prod_idprod_nameprod_price
    a0018sockets9.49
    a0019iphone13600
    b0019gucci t-shirts1000

    【问题】编写 SQL 语句,从 Products 表中检索产品 ID(prod_id)和产品名称(prod_name),只返回价格为 9 元或更高的产品。查询结果如下:

    prod_idprod_name
    a0018sockets
    a0019iphone13
    b0019gucci 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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    47、使用条件过滤数据(3)

    有数据表:Products,表中数据如下:

    prod_idprod_nameprod_price
    a0011egg3
    a0019sockets4
    b0019coffee15

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

    prod_nameprod_price
    egg3
    sockets4

    表结构及数据如下:

    /*
    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);
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    解答:

    /*
    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    48、使用条件过滤数据(4)

    OrderItems 表含有:订单号 order_num,quantity 产品数量,表中数据如下:

    order_numquantity
    a1105
    a21100
    a2200
    a41121
    a510
    a219
    a75

    【问题】从 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');
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    解答:

    mysql> select distinct order_num from OrderItems where quantity >= 100;
    +-----------+
    | order_num |
    +-----------+
    | a1        |
    | a2        |
    | a4        |
    +-----------+
    3 rows in set (0.00 sec)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    49、使用条件过滤数据(5)

    Vendors 表有字段供应商名称(vend_name)、供应商国家(vend_country)、供应商州(vend_state),表中数据如下:

    vend_namevend_countryvend_state
    appleUSACA
    vivoCNAshenzhen
    huaweiCNAxian

    【问题】编写 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');
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    解答:

    mysql> select vend_name from Vendors where vend_country = 'USA' and vend_state = 'CA';
    +-----------+
    | vend_name |
    +-----------+
    | apple     |
    +-----------+
    1 row in set (0.00 sec)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    50、使用条件过滤数据(6)

    OrderItems 表包含了所有已订购的产品(有些产品被订购多次)。

    prod_idorder_numquantity
    BR01a1105
    BR02a21100
    BR02a2200
    BR03a41121
    BR017a510
    BR02a219
    BR017a75

    【问题】编写 SQL 语句,查找所有订购了数量至少100 个的 BR01、BR02 或 BR03 的订单。返回 OrderItems 表的订单号(order_num)、产品 ID(prod_id)和数量(quantity)。查询结果如下:

    order_numprod_idquantity
    a1BR01105
    a2BR021100
    a2BR02200
    a4BR031121

    表结构及数据如下:

    /*
    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');
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    解答:

    /*
    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
  • 相关阅读:
    食堂消费开启新模式,接招吧
    dropwizard中上传和下载文件
    【vue2第十一章】v-model的原理详解 与 如何使用v-model对父子组件的value绑定 和修饰符.sync
    基于JSP的动漫论坛
    【【VDMA彩条显示实验之四 含C语言代码】】
    Ubuntu22.04 & Win11 双系统hibernate冷切换实现
    Vue | 模板语法、数据绑定、data与el的两种写法
    ARM 按键控制 LED灯,蜂鸣器,风扇
    图书馆座位预约系统
    PDF处理还收费?不可能
  • 原文地址:https://blog.csdn.net/weixin_44377973/article/details/126033116