数据库经典面试题

发布时间:2024年01月19日

习题一

1.1 创建表

①创建Student表

mysql> create table Student (
? ? -> Sno int primary key,
? ? -> Sname varchar(255),
? ? -> Ssex varchar(10),
? ? -> Sdept varchar(50)
? ? -> );
Query OK, 0 rows affected (0.01 sec)

②创建Course表

mysql> create table Course (
? ? -> Cno int primary key,
? ? -> Cname varchar(255),
? ? -> Tno int
? ? -> );
Query OK, 0 rows affected (0.01 sec)?

③创建Score表

mysql> create table Score (
? ? -> Sno int,
? ? -> Cno int,
? ? -> Degree int,
? ? -> primary key (Sno,Cno)
? ? -> );
Query OK, 0 rows affected (0.01 sec)?

④创建Teacher表

mysql> create table Teacher (
? ? -> Tno int primary key,
? ? -> Tname varchar(255),
? ? -> Tsex varchar(10),
? ? -> Prof varchar(50)
? ? -> );
Query OK, 0 rows affected (0.00 sec)?

?1.2?在Student表中插入一条记录

①命令

mysql> insert into Student values(1,'张三','男','计科2003班');
Query OK, 1 row affected (0.01 sec)

②结果

1.3?查询课程名称为”数据库”,且分数低于60的学生姓名和分数?

#方法一
mysql> select s.Sname
    -> from Student s
    -> inner join Score SC on s.Sno = SC.Sno
    -> inner join Course c on SC.Cno = c.Cno
    -> where c.Cname = '数据库' and SC.Degree < 60;
Empty set (0.01 sec)
#方法二
mysql> select s.Sname,SC.Degree
    -> from Student s
    -> inner join Score SC on s.Sno = SC.Sno
    -> inner join Course c on SC.Cno = c.Cno
    -> where c.Cname = '数据库' and SC.Degree < 60;
Empty set (0.00 sec)
#方法三
mysql> select s.Sname, SC.Degree, C.Cname
    -> from Student s
    -> inner join Score SC on s.Sno = SC.Sno
    -> inner join Course C on SC.Cno = C.Cno
    -> where C.Cname = '数据库' and SC.Degree < 60;
Empty set (0.00 sec)

1.4?对于所有性别为“女”的学生,同时课程名为“高等数学”的分数统一加5?

mysql> update Score SC
    -> set Degree = Degree + 5
    -> where SC.Sno in (select s.Sno from Student s where s.Ssex = '女')
    -> and SC.Cno in (select c.Cno from Course c where c.Cname = '高等数学');
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

1.5?删除姓名为“张”(学号=1) 课程名为”数据库”的课程成绩?

mysql> delete from Score
    -> where Sno = 1
    -> and Cno in (select c.Cno from Course c where c.Cname = '数据库');
Query OK, 0 rows affected (0.01 sec)

习题二

2.1?统计2021年11月每天新用户的次日留存率(保留2位小数)

①命令

mysql> DROP TABLE IF EXISTS tb_user_log;
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE tb_user_log (
    ->     id INT PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
    ->     uid INT NOT NULL COMMENT '用户ID',
    ->     artical_id INT NOT NULL COMMENT '视频ID',
    ->     in_time datetime COMMENT '进入时间',
    ->     out_time datetime COMMENT '离开时间',
    ->     sign_in TINYINT DEFAULT 0 COMMENT '是否签到'
    -> ) CHARACTER SET utf8 COLLATE utf8_bin;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> INSERT INTO tb_user_log(uid, artical_id, in_time, out_time, sign_in)
    -> VALUES
    -> (101,0,'2021-11-01 10:00:00','2021-11-01 10:00:42',1),
    -> (102,9001,'2021-11-01 10:00:00','2021-11-01 10:00:09',0),
    -> (103,9001,'2021-11-01 10:00:01','2021-11-01 10:01:50',0),
    -> (101,9002,'2021-11-02 10:00:09','2021-11-02 10:00:28',0),
    -> (103,9002,'2021-11-02 10:00:51','2021-11-02 10:00:59',0),
    -> (104,9001,'2021-11-02 10:00:28','2021-11-02 10:00:50',0),
    -> (101,9003,'2021-11-03 11:00:55','2021-11-03 11:01:24',0),
    -> (104,9003,'2021-11-03 11:00:45','2021-11-03 11:00:55',0),
    -> (105,9003,'2021-11-03 11:00:53','2021-11-03 11:00:59',0),
    -> (101,9002,'2021-11-04 11:00:55','2021-11-04 11:00:59',0);
Query OK, 10 rows affected (0.01 sec)
Records: 10  Duplicates: 0  Warnings: 0

②结果

2.2?编写SQL语句,查找所有订购了数量至少100个的BRO1、BR02或BRO3的订单。你需要返回 Orderltems表的订单号(order num) 、产品ID(prod id)和数量(quantity) ,并按产品ID和数量进行过滤?

①命令

mysql> DROP TABLE IF EXISTS `Orderltems`;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `Orderltems` (
    -> prod_id VARCHAR(255) NOT NULL COMMENT '商品号',
    -> order_num VARCHAR(255) NOT NULL COMMENT '商品订单号',
    -> quantity INT(255) NOT NULL COMMENT '商品数量'
    -> );
Query OK, 0 rows affected, 1 warning (0.02 sec)

mysql> INSERT `Orderltems` VALUES('BRO1','a','105'),('BRO2','a2','1100'),('BR02','a2','200'),('BR04','a4','1121'),('BR017','a5','10'),('BR02','a2','19'),('BR017','a','75');
Query OK, 7 rows affected (0.00 sec)
Records: 7  Duplicates: 0  Warnings: 0

②结果

2.3?编写SQL语句,从Products表中检索所有的产品名称(prod name) ,以及名为quant sold的计算列,其中包含所售产品的总数(在Orderltems表上使用子查询和SUM(quantity)检索)?

①命令

mysql> DROP TABLE IF EXISTS `Products`;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `Products` (
    -> `prod_id` VARCHAR(255) NOT NULL COMMENT '产品ID',
    -> `prod_name` VARCHAR(255) NOT NULL COMMENT '产品名称'
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO `Products` VALUES ('a0001','egg'),
    -> ('a0002','sockets'),
    -> ('a0013','coffee'),
    -> ('a0003','cola');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0
mysql> DROP TABLE IF EXISTS `Orderltems`;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `Orderltems` (
    -> prod_id VARCHAR(255) NOT NULL COMMENT '产品id',
    -> quantity INT(16) NOT NULL COMMENT '商品数量'
    -> );
Query OK, 0 rows affected, 1 warning (0.02 sec)

mysql> INSERT INTO `Orderltems` VALUES ('a0001',105),('a0002',1100),('a0002',200),('a0013',1121),('a0003',10),('a0003',19),('a0003',5);
Query OK, 7 rows affected (0.00 sec)
Records: 7  Duplicates: 0  Warnings: 0

②结果

2.4 检索每个顾客的名称 (Customers表中的 cust name) 和所有的订单号(Orders 表中的 order num),列出所有的顾客,即使他们没有下过订单。最后根据顾客姓名cust name升序返回

①命令

mysql> DROP TABLE IF EXISTS `Customers`;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `Customers` (
    -> cust_id VARCHAR(255) NOT NULL COMMENT '客户id',
    -> cust_name VARCHAR(255) NOT NULL COMMENT '客户姓名'
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> INSERT `Customers` VALUES ('cust10','andy'),('cust1','ben'),('cust2' ,'tony'),('cust22','tom'),('cust221','an'),('cust2217','hex'),('cust40','ace');
Query OK, 7 rows affected (0.00 sec)
Records: 7  Duplicates: 0  Warnings: 0
mysql> DROP TABLE IF EXISTS `Orders`;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `Orders` (
    -> order_num VARCHAR(255) NOT NULL COMMENT '商品单号',
    -> cust_id VARCHAR(255) NOT NULL COMMENT '顾客id'
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT `Orders` VALUES ('a1','cust10'),('a2','cust1'),('a3','cust2'),('a4','cust22'),('a5','cust221'),('a7','cust22171');
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

②结果

2.5 请统计2021年10月每个有展示记录的退货率不大于0.5的商品各项指标?

①命令

mysql> DROP TABLE IF EXISTS tb_user_event;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE tb_user_event (
    ->     id INT PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
    ->     uid INT NOT NULL COMMENT '用户ID',
    ->     product_id INT NOT NULL COMMENT '商品ID',
    ->     event_time datetime COMMENT '行为时间',
    ->     if_click TINYINT COMMENT '是否点击',
    ->     if_cart TINYINT COMMENT '是否加购物车',
    ->     if_payment TINYINT COMMENT '是否付款',
    ->     if_refund TINYINT COMMENT '是否退货退款'
    -> ) CHARACTER SET utf8 COLLATE utf8_bin;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

mysql> INSERT INTO tb_user_event (uid, product_id, event_time, if_click, if_cart, if_payment, if_refund) VALUES
    ->     (101,8001,'2021-10-01 10:00:00',0,0,0,0),
    ->     (102,8001,'2021-10-01 10:00:00',1,0,0,0),
    ->     (103,8001,'2021-10-01 10:00:00',1,1,0,0),
    ->     (104,8001,'2021-10-02 10:00:00',1,1,1,0),
    ->     (105,8001,'2021-10-02 10:00:00',1,1,1,0),
    ->     (101,8002,'2021-10-03 10:00:00',1,1,1,0),
    ->     (109,8001,'2021-10-04 10:00:00',1,1,1,1);
Query OK, 7 rows affected (0.00 sec)
Records: 7  Duplicates: 0  Warnings: 0

②结果

文章来源:https://blog.csdn.net/2201_75581284/article/details/135684190
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。