博主昵称:跳楼梯企鹅
博主主页面链接:博主主页传送门博主专栏页面连接:专栏传送门--网路安全技术
创作初心:本博客的初心为与技术朋友们相互交流,每个人的技术都存在短板,博主也是一样,虚心求教,希望各位技术友给予指导。
博主座右铭:发现光,追随光,成为光,散发光;
博主研究方向:渗透测试、机器学习 ;
博主寄语:感谢各位技术友的支持,您的支持就是我前进的动力 ;学习网站跳转链接:牛客刷题网
给大家推荐一款很好的刷题软件牛客刷题网---一起学SQL

博主为什么喜欢用这个网站学习呢?
主要原因有三点:
1.内部含有大量面试题库
2.覆盖行业范围比较全面
3.刷题的题目是按照简单到难的过程
子查询是嵌套在另一个语句,如:select,insert,update、delete中的查询
子查询可以嵌套在另外一个子查询中,SQL Server最多支持32个嵌套级别
①相关子查询是使用外部查询的值的子查询。即,它取决于外部查询的值
②相关子查询不能作为简单子查询独立执行
③对外部查询评估的每一行重复执行一次相关子查询。相关子查询也称为重复子查询。
题目:返回购买价格为 10 美元或以上产品的顾客列表
描述:OrderItems表示订单商品表,含有字段订单号:order_num、订单价格:item_price;Orders表代表订单信息表,含有顾客id:cust_id和订单号:order_num

- 输入:
-
- DROP TABLE IF EXISTS `OrderItems`;
-
- CREATE TABLE IF NOT EXISTS `OrderItems`(
-
- order_num VARCHAR(255) NOT NULL COMMENT '商品订单号',
-
- item_price INT(16) NOT NULL COMMENT '售出价格'
-
- );
-
- INSERT `OrderItems` VALUES ('a1',10),('a2',1),('a2',1),('a4',2),('a5',5),('a2',1),('a7',7);
-
-
- DROP TABLE IF EXISTS `Orders`;
-
- CREATE TABLE IF NOT EXISTS `Orders`(
-
- order_num VARCHAR(255) NOT NULL COMMENT '商品订单号',
-
- cust_id VARCHAR(255) NOT NULL COMMENT '顾客id'
-
- );
-
- INSERT `Orders` VALUES ('a1','cust10'),('a2','cust1'),('a2','cust1'),('a4','cust2'),('a5','cust5'),('a2','cust1'),('a7','cust7');
-
-
- 输出:
-
-
- cust10
- select cust_idfrom Orderswhere order_num in (
-
- select order_num
-
- from OrderItems
-
- where item_price >=10
-
- )

题目:确定哪些订单购买了 prod_id 为 BR01 的产品(一)
描述:表OrderItems代表订单商品信息表,prod_id为产品id;Orders表代表订单表有cust_id代表顾客id和订单日期order_date

- 输入:
-
- DROP TABLE IF EXISTS `OrderItems`;
-
- CREATE TABLE IF NOT EXISTS `OrderItems`(
-
- prod_id VARCHAR(255) NOT NULL COMMENT '产品id',
-
- order_num VARCHAR(255) NOT NULL COMMENT '商品订单号'
-
- );
-
- INSERT `OrderItems` VALUES ('BR01','a0001'),('BR01','a0002'),('BR02','a0003'),('BR02','a0013');
-
-
- DROP TABLE IF EXISTS `Orders`;
-
- CREATE TABLE IF NOT EXISTS `Orders`(
-
- order_num VARCHAR(255) NOT NULL COMMENT '商品订单号',
-
- cust_id VARCHAR(255) NOT NULL COMMENT '顾客id',
-
- order_date TIMESTAMP NOT NULL COMMENT '下单时间'
-
- );
-
- INSERT `Orders` VALUES ('a0001','cust10','2022-01-01 00:00:00'),('a0002','cust1','2022-01-01 00:01:00'),('a0003','cust1','2022-01-02 00:00:00'),('a0013','cust2','2022-01-01 00:20:00');
-
-
- 输出:
-
-
- cust10|2022-01-01 00:00:00
-
- cust1|2022-01-01 00:01:00
- select cust_id,order_datefrom Orderswhere order_num in
-
- (select order_numfrom OrderItemswhere prod_id = 'BR01')

题目:返回购买 prod_id 为 BR01 的产品的所有顾客的电子邮件(一)
描述:你想知道订购 BR01 产品的日期,有表OrderItems代表订单商品信息表,prod_id为产品id;Orders表代表订单表有cust_id代表顾客id和订单日期order_date;Customers表含有cust_email 顾客邮件和cust_id顾客id

- 输入:
-
- DROP TABLE IF EXISTS `OrderItems`;
-
- CREATE TABLE IF NOT EXISTS `OrderItems`(
-
- prod_id VARCHAR(255) NOT NULL COMMENT '产品id',
-
- order_num VARCHAR(255) NOT NULL COMMENT '商品订单号'
-
- );
-
- INSERT `OrderItems` VALUES ('BR01','a0001'),('BR01','a0002'),('BR02','a0003'),('BR02','a0013');
-
-
- DROP TABLE IF EXISTS `Orders`;
-
- CREATE TABLE IF NOT EXISTS `Orders`(
-
- order_num VARCHAR(255) NOT NULL COMMENT '商品订单号',
-
- cust_id VARCHAR(255) NOT NULL COMMENT '顾客id',
-
- order_date TIMESTAMP NOT NULL COMMENT '下单时间'
-
- );
-
- INSERT `Orders` VALUES ('a0001','cust10','2022-01-01 00:00:00'),('a0002','cust1','2022-01-01 00:01:00'),('a0003','cust1','2022-01-02 00:00:00'),('a0013','cust2','2022-01-01 00:20:00');
-
- DROP TABLE IF EXISTS `Customers`;CREATE TABLE IF NOT EXISTS `Customers`(
-
- cust_id VARCHAR(255) NOT NULL COMMENT '顾客id',
-
- cust_email VARCHAR(255) NOT NULL COMMENT '顾客email'
-
- );INSERT `Customers` VALUES ('cust10','cust10@cust.com'),('cust1','cust1@cust.com'),('cust2','cust2@cust.com');
-
-
- 输出:
-
-
- cust10@cust.com
-
- cust1@cust.com
- select
-
- Customers.cust_emailfrom
-
- Ordersleft join OrderItems on OrderItems.order_num = Orders.order_numleft join Customers on Customers.cust_id = Orders.cust_idwhere
-
- OrderItems.prod_id = "BR01"

题目:返回每个顾客不同订单的总金额
描述:我们需要一个顾客 ID 列表,其中包含他们已订购的总金额。OrderItems表代表订单信息,OrderItems表有订单号:order_num和商品售出价格:item_price、商品数量:quantity。

- 输入:
-
- DROP TABLE IF EXISTS `OrderItems`;CREATE TABLE IF NOT EXISTS `OrderItems`(
-
- order_num VARCHAR(255) NOT NULL COMMENT '商品订单号',
-
- item_price INT(16) NOT NULL COMMENT '售出价格',
-
- quantity INT(16) NOT NULL COMMENT '商品数量'
-
- );INSERT `OrderItems` VALUES ('a0001',10,105),('a0002',1,1100),('a0002',1,200),('a0013',2,1121),('a0003',5,10),('a0003',1,19),('a0003',7,5);
-
- DROP TABLE IF EXISTS `Orders`;CREATE TABLE IF NOT EXISTS `Orders`(
-
- order_num VARCHAR(255) NOT NULL COMMENT '商品订单号',
-
- cust_id VARCHAR(255) NOT NULL COMMENT '顾客id'
-
- );INSERT `Orders` VALUES ('a0001','cust10'),('a0003','cust1'),('a0013','cust2');
-
-
- 输出:
-
-
- cust2|2242.000
-
- cust10|1050.000
-
- cust1|104.000
- SELECT
-
- o.cust_id,
-
- sum(oi.item_price * oi.quantity) AS total_orderedFROM Orders o JOIN OrderItems oi
-
- USING (order_num)GROUP BY o.cust_idORDER BY total_ordered DESC

题目:从 Products 表中检索所有的产品名称以及对应的销售总数
描述: Products 表中检索所有的产品名称:prod_name、产品id:prod_id
OrderItems代表订单商品表,订单产品:prod_id、售出数量:quantity

输入:
- 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 '产品名称'
-
- );INSERT INTO `Products` VALUES ('a0001','egg'),
-
- ('a0002','sockets'),
-
- ('a0013','coffee'),
-
- ('a0003','cola');
-
- DROP TABLE IF EXISTS `OrderItems`;CREATE TABLE IF NOT EXISTS `OrderItems`(
-
- prod_id VARCHAR(255) NOT NULL COMMENT '产品id',
-
- quantity INT(16) NOT NULL COMMENT '商品数量'
-
- );INSERT `OrderItems` VALUES ('a0001',105),('a0002',1100),('a0002',200),('a0013',1121),('a0003',10),('a0003',19),('a0003',5);
-
-
- 输出:
-
-
- egg|105.000
-
- sockets|1300.000
-
- coffee|1121.000
-
- cola|34.000
- select
-
- prod_name,
-
- sum(quantity) as quant_soldfrom
-
- Products,
-
- OrderItemswhere
-
- OrderItems.prod_id = Products.prod_idgroup by
-
- prod_name

本篇文章为SQL刷题第十四天
欢迎各位小伙伴点击右边链接和博主一起学习点击学习