※食用指南:文章内容为牛客网《SQL必知必会》51道题重点笔记,用于重复思考错题,加深印象。
本文章涉及题目也是《SQL必知必会》书中“挑战题”,题目及答案:《SQL必知必会》随书习题答案
练习传送门:SQL必知必会51题
目录:
SQL92、SQL98 确定哪些订单购买了 prod_id 为 BR01 的产品
SQL93、SQL99 返回购买 prod_id 为 BR01 的产品的所有顾客的电子邮件
SQL95 从 Products 表中检索所有的产品名称以及对应的销售总数
SQL106、SQL107 将两个 SELECT 语句结合起来
SQL72 检索并列出已订购产品的清单
❓查找所有订购了数量至少100 个的 BR01、BR02 或BR03 的订单(按产品 ID 和数量进行过滤)
SELECT order_num,prod_id,quantity
FROM OrderItems
WHERE prod_id IN ('BR01','BR02','BR03') AND
quantity > 100
ORDER BY prod_id,quantity DESC
SQL78 检索产品名称和描述(四)
❓返回在描述中以先后顺序同时出现 toy 和 carrots 的产品
SELECT prod_name,prod_desc
FROM Products
WHERE prod_desc LIKE '%toy%carrots%'
SQL81 顾客登录名
❓返回顾客 ID(cust_id)、顾客名称(cust_name)和登录名(user_login)
其中登录名全部为大写字母,并由顾客联系人的前两个字符(cust_contact)和其所在城市的前三个字符(cust_city)组成
SUBSTRING:字符串的截取(字符串,起始位置,截取字符数)
CONCAT:字符串的拼接(字符串1,字符串2,字符串3,...)
LEFT:取字符串左边(字符串,截取字符数)
UPPER:字母大写(字符串)
LOWER:字母小写(字符串)
SELECT cust_id,
cust_name,
UPPER(CONCAT(SUBSTRING(cust_contact, 1, 2),
SUBSTRING(cust_city, 1, 3))) AS user_login
FROM Customers
SELECT cust_id,cust_name,
UPPER(CONCAT(LEFT(cust_contact,2),LEFT(cust_city,3)))
AS user_login
FROM Customers
SQL86 返回每个订单号各有多少行数
❓返回每个订单号(order_num)各有多少行数(order_lines),并按 order_lines对结果进行升序排序
①COUNTt(*),COUNT(列名)都可以,区别在于,COUNT(列名)是统计非NULL的行数
②ORDER BY最后执行,所以可以使用列别名
③分组聚合一定不要忘记加上GROUP BY ,不然只会有一行结果
SELECT order_num,COUNT(order_num) AS order_lines
FROM OrderItems
GROUP BY order_num
ORDER BY order_lines
SQL89、SQL100 确定最佳顾客的另一种方式
❓返回订单总价不小于1000 的所有订单号,最后的结果按订单号进行升序排序
SELECT order_num,SUM(item_price*quantity) AS total_price
FROM OrderItems
GROUP BY order_num
HAVING total_price >= 1000
ORDER BY order_num
SELECT cust_name,
SUM(item_price*quantity) AS total_price
FROM OrderItems OI INNER JOIN Orders O ON OI.order_num = O.order_num
INNER JOIN Customers C ON O.cust_id = C.cust_id
GROUP BY cust_name
HAVING total_price >= 100
ORDER BY total_price
SQL92、SQL98 确定哪些订单购买了 prod_id 为 BR01 的产品
❓哪些订单(在 OrderItems 中)购买了 prod_id 为 "BR01" 的产品,然后从 Orders 表中返回每个产品对应的顾客 ID(cust_id)和订单日期(order_date),按订购日期对结果进行升序排序
SELECT cust_id,order_date
FROM Orders
WHERE order_num IN (SELECT order_num
FROM OrderItems
WHERE prod_id = 'BR01')
ORDER BY order_date
SELECT cust_id,order_date
FROM Orders O
JOIN OrderItems OI ON O.order_num = OI.order_num
WHERE prod_id = 'BR01'
ORDER BY order_date
SQL93、SQL99 返回购买 prod_id 为 BR01 的产品的所有顾客的电子邮件
❓返回购买 prod_id 为BR01 的产品的所有顾客的电子邮件(Customers 表中的 cust_email)
SELECT cust_email
FROM Customers
WHERE cust_id IN (SELECT cust_id
FROM Orders
WHERE order_num IN (SELECT order_num
FROM OrderItems
WHERE prod_id = 'BR01' ))
SELECT cust_email
FROM Orders O INNER JOIN OrderItems OI ON O.order_num = OI.order_num
INNER JOIN Customers C ON O.cust_id = C.cust_id
WHERE prod_id = 'BR01'
SQL94 返回每个顾客不同订单的总金额
❓返回顾客 ID(Orders 表中的 cust_id),并使用子查询返回total_ordered 以便返回每个顾客的订单总数,将结果按金额从大到小排序
SELECT cust_id,
SUM((SELECT SUM(item_price * quantity)
FROM OrderItems
WHERE OrderItems.order_num = Orders.order_num
GROUP BY order_num)) AS total_ordered
FROM Orders
GROUP BY cust_id
ORDER BY total_ordered DESC;
SQL95 从 Products 表中检索所有的产品名称以及对应的销售总数
❓检索所有的产品名称(prod_name),以及名为 quant_sold 的计算列,其中包含所售产品的总数(在 OrderItems 表上使用子查询和 SUM(quantity)检索)
SELECT prod_name,
(SELECT SUM(quantity)
FROM OrderItems
WHERE OrderItems.prod_id = Products.prod_id
GROUP BY prod_id) AS quant_sold
FROM Products
SQL97 返回顾客名称和相关订单号以及每个订单的总价
❓返回 Customers 表中的顾客名称(cust_name)和Orders 表中的相关订单号(order_num),添加第三列 OrderTotal,其中包含每个订单的总价,并按顾客名称再按订单号对结果进行升序排序
SELECT C.cust_name,
O.order_num,
SUM(OI.quantity*OI.item_price) AS OrderTotal
FROM Orders O
JOIN OrderItems OI ON O.order_num = OI.order_num
JOIN Customers C ON C.cust_id = O.cust_id
GROUP BY C.cust_name,O.order_num
ORDER BY C.cust_name,O.order_num
SQL102 检索每个顾客的名称和所有的订单号(二)
❓检索每个顾客的名称(Customers表中的 cust_name)和所有的订单号(Orders 表中的 order_num),列出所有的顾客,即使他们没有下过订单。最后根据顾客姓名cust_name升序返回
SELECT cust_name,order_num
FROM Customers C LEFT JOIN Orders O USING (cust_id)
ORDER BY cust_name
SQL103 返回产品名称和与之相关的订单号
❓使用 OUTER JOIN 联结 Products 表和 OrderItems 表,返回产品名称(prod_name)和与之相关的订单号(order_num)的列表,并按照产品名称升序排序
SELECT prod_name,order_num
FROM Products P LEFT JOIN OrderItems OI ON P.prod_id = OI.prod_id
UNION
(SELECT prod_name,order_num
FROM Products P RIGHT JOIN OrderItems OI ON P.prod_id = OI.prod_id)
ORDER BY prod_name
SQL105 列出供应商及其可供产品的数量
❓列出供应商(Vendors 表中的 vend_id)及其可供产品的数量,包括没有产品的供应商。你需要使用 OUTER JOIN 和 COUNT()聚合函数来计算 Products 表中每种产品的数量,最后根据vend_id 升序排序
SELECT V.vend_id,COUNT(P.prod_id) AS prod_id
FROM Vendors V LEFT JOIN Products P ON V.vend_id = P.vend_id
GROUP BY V.vend_id
ORDER BY V.vend_id
SQL106、SQL107 将两个 SELECT 语句结合起来
❓将两个 SELECT 语句结合起来,以便从 OrderItems表中检索产品 id(prod_id)和 quantity。其中,一个 SELECT 语句过滤数量为 100 的行,另一个 SELECT 语句过滤 id 以 BNBG 开头的产品,最后按产品 id 对结果进行升序排序
SELECT prod_id,quantity
FROM OrderItems
WHERE prod_id LIKE 'BNBG%'
UNION
SELECT prod_id,quantity
FROM OrderItems
WHERE quantity = 100
ORDER BY prod_id
SELECT prod_id,quantity
FROM OrderItems
WHERE prod_id LIKE 'BNBG%' OR quantity = 100
ORDER BY prod_id
SQL109 纠错4
❓返回顾客名称cust_name、顾客联系方式cust_contact、顾客email cust_email
表Customers含有字段cust_name顾客名、cust_contact顾客联系方式、cust_state顾客州、cust_email顾客email
使用union组合查询时,只能使用一条order by字句,他必须位于最后一条select语句之后,因为对于结果集不存在对于一部分数据进行排序,而另一部分用另一种排序规则的情况
SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust_state = 'MI'
UNION
SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust_state = 'IL'
ORDER BY cust_name
————END