101. 患某种疾病的患者
写一条 SQL 语句,查询患有 I 类糖尿病的患者 ID (patient_id)、患者姓名(patient_name)以及其患有的所有疾病代码(conditions)。I 类糖尿病的代码总是包含前缀 DIAB1 。
按任意顺序返回结果表。
+------------+--------------+--------------+| patient_id | patient_name | conditions |+------------+--------------+--------------+| 3 | Bob | DIAB100 MYOP || 4 | George | ACNE DIAB100 |+------------+--------------+--------------+Bob and George both have a condition that starts with DIAB1.
Create table If Not Exists 101_Patients (patient_id int, patient_name varchar(30), conditions varchar(100));Truncate table 101_Patients;insert into 101_Patients (patient_id, patient_name, conditions) values ('1', 'Daniel', 'YFEV COUGH');insert into 101_Patients (patient_id, patient_name, conditions) values ('2', 'Alice', '');insert into 101_Patients (patient_id, patient_name, conditions) values ('3', 'Bob', 'DIAB100 MYOP');insert into 101_Patients (patient_id, patient_name, conditions) values ('4', 'George', 'ACNE DIAB100');insert into 101_Patients (patient_id, patient_name, conditions) values ('5', 'Alain', 'DIAB201');
最终SQL:
selectpatient_id,patient_name,conditionsfromPatientswhereconditions like '%DIAB1%';
102. 最近的三笔订单
写一个 SQL 语句,找到每个用户的最近三笔订单。如果用户的订单少于 3 笔,则返回他的全部订单。
返回的结果按照 customer_name 升序排列。如果排名有相同,则继续按照 customer_id 升序排列。如果排名还有相同,则继续按照 order_date 升序排列。
+---------------+-------------+----------+------------+| customer_name | customer_id | order_id | order_date |+---------------+-------------+----------+------------+| Annabelle | 3 | 7 | 2020-08-01 || Annabelle | 3 | 3 | 2020-07-31 || Jonathan | 2 | 9 | 2020-08-07 || Jonathan | 2 | 6 | 2020-08-01 || Jonathan | 2 | 2 | 2020-07-30 || Marwan | 4 | 4 | 2020-07-29 || Winston | 1 | 8 | 2020-08-03 || Winston | 1 | 1 | 2020-07-31 || Winston | 1 | 10 | 2020-07-15 |+---------------+-------------+----------+------------+
Create table If Not Exists 102_Customers (customer_id int, name varchar(10));Create table If Not Exists 102_Orders (order_id int, order_date date, customer_id int, cost int);Truncate table 102_Customers;insert into 102_Customers (customer_id, name) values ('1', 'Winston');insert into 102_Customers (customer_id, name) values ('2', 'Jonathan');insert into 102_Customers (customer_id, name) values ('3', 'Annabelle');insert into 102_Customers (customer_id, name) values ('4', 'Marwan');insert into 102_Customers (customer_id, name) values ('5', 'Khaled');Truncate table 102_Orders;insert into 102_Orders (order_id, order_date, customer_id, cost) values ('1', '2020-07-31', '1', '30');insert into 102_Orders (order_id, order_date, customer_id, cost) values ('2', '2020-7-30', '2', '40');insert into 102_Orders (order_id, order_date, customer_id, cost) values ('3', '2020-07-31', '3', '70');insert into 102_Orders (order_id, order_date, customer_id, cost) values ('4', '2020-07-29', '4', '100');insert into 102_Orders (order_id, order_date, customer_id, cost) values ('5', '2020-06-10', '1', '1010');insert into 102_Orders (order_id, order_date, customer_id, cost) values ('6', '2020-08-01', '2', '102');insert into 102_Orders (order_id, order_date, customer_id, cost) values ('7', '2020-08-01', '3', '111');insert into 102_Orders (order_id, order_date, customer_id, cost) values ('8', '2020-08-03', '1', '99');insert into 102_Orders (order_id, order_date, customer_id, cost) values ('9', '2020-08-07', '2', '32');insert into 102_Orders (order_id, order_date, customer_id, cost) values ('10', '2020-07-15', '1', '2');
最终SQL:
-- 方法一selectC.name as customer_name,O.customer_id,O.order_id,O.order_datefrom102_customers Cinner join102_Orders OonC.customer_id = O.customer_idwhere(O.customer_id,O.order_date) in (selecto1.customer_id,o1.order_datefrom102_Orders o1inner join102_Orders o2ono1.customer_id = o2.customer_idando1.order_date <= o2.order_dategroup byo1.customer_id,o1.order_datehavingcount(o2.order_date) <= 3)order bycustomer_name,O.customer_id,O.order_date desc;-- 方法二selectname customer_name,customer_id,order_id,order_datefrom(selectname ,o.customer_id,order_id,order_date ,rank() over(partition by o.customer_id order by order_date desc) rkfrom102_Orders oleft join102_Customers cono.customer_id=c.customer_id )t1where rk <=3order by customer_name ,customer_id,order_date desc;
103. 格式化产品名称
写一个 SQL 语句报告:
product_name 是小写字母且不包含前后空格
sale_date 格式为 (‘YYYY-MM’)
total 是产品在本月销售的次数
返回结果以 product_name 升序 排列,如果有排名相同, 再以 sale_date 升序 排列。
+--------------+--------------+----------+| product_name | sale_date | total |+--------------+--------------+----------+| lckeychain | 2000-02 | 2 || lcphone | 2000-01 | 2 || lcphone | 2000-02 | 1 || matryoshka | 2000-03 | 1 |+--------------+--------------+----------+In January, 2 LcPhones were sold, please note that the product names are not case sensitive and may contain spaces.In Februery, 2 LCKeychains and 1 LCPhone were sold.In March, 1 matryoshka was sold.
Create table If Not Exists 103_Sales (sale_id int, product_name varchar(30), sale_date date);Truncate table 103_Sales;insert into 103_Sales (sale_id, product_name, sale_date) values ('1', 'LCPHONE', '2000-01-16');insert into 103_Sales (sale_id, product_name, sale_date) values ('2', 'LCPhone', '2000-01-17');insert into 103_Sales (sale_id, product_name, sale_date) values ('3', 'LcPhOnE', '2000-02-18');insert into 103_Sales (sale_id, product_name, sale_date) values ('4', 'LCKeyCHAiN', '2000-02-19');insert into 103_Sales (sale_id, product_name, sale_date) values ('5', 'LCKeyChain', '2000-02-28');insert into 103_Sales (sale_id, product_name, sale_date) values ('6', 'Matryoshka', '2000-03-31');
最终SQL:
selecttrim(lower(product_name)) as product_name,date_format(sale_date,'%Y-%m') as sale_date,count(*) as totalfrom103_Salesgroup bytrim(lower(product_name)),date_format(sale_date,'%Y-%m')order byproduct_name asc,sale_date asc;
104. 每个商品最近的订单
编写一个SQL查询来查找每个产品的最新订单。返回按product_name升序排序的结果表,如果有并列,则按product_id升序排序。如果仍然存在并列,则按order_id升序对它们排序。
效果展示:
+--------------+------------+----------+------------+| product_name | product_id | order_id | order_date |+--------------+------------+----------+------------+| keyboard | 1 | 6 | 2020-08-01 || keyboard | 1 | 7 | 2020-08-01 || mouse | 2 | 8 | 2020-08-03 || screen | 3 | 3 | 2020-08-29 |+--------------+------------+----------+------------+
建表语句:
Create table If Not Exists 104_Customers (customer_id int, name varchar(10));Create table If Not Exists 104_Orders (order_id int, order_date date, customer_id int, product_id int);Create table If Not Exists 104_Products (product_id int, product_name varchar(20), price int);Truncate table 104_Customers;insert into 104_Customers (customer_id, name) values ('1', 'Winston');insert into 104_Customers (customer_id, name) values ('2', 'Jonathan');insert into 104_Customers (customer_id, name) values ('3', 'Annabelle');insert into 104_Customers (customer_id, name) values ('4', 'Marwan');insert into 104_Customers (customer_id, name) values ('5', 'Khaled');Truncate table 104_Orders;insert into 104_Orders (order_id, order_date, customer_id, product_id) values ('1', '2020-07-31', '1', '1');insert into 104_Orders (order_id, order_date, customer_id, product_id) values ('2', '2020-7-30', '2', '2');insert into 104_Orders (order_id, order_date, customer_id, product_id) values ('3', '2020-08-29', '3', '3');insert into 104_Orders (order_id, order_date, customer_id, product_id) values ('4', '2020-07-29', '4', '1');insert into 104_Orders (order_id, order_date, customer_id, product_id) values ('5', '2020-06-10', '1', '2');insert into 104_Orders (order_id, order_date, customer_id, product_id) values ('6', '2020-08-01', '2', '1');insert into 104_Orders (order_id, order_date, customer_id, product_id) values ('7', '2020-08-01', '3', '1');insert into 104_Orders (order_id, order_date, customer_id, product_id) values ('8', '2020-08-03', '1', '2');insert into 104_Orders (order_id, order_date, customer_id, product_id) values ('9', '2020-08-07', '2', '3');insert into 104_Orders (order_id, order_date, customer_id, product_id) values ('10', '2020-07-15', '1', '2');Truncate table 104_Products;insert into 104_Products (product_id, product_name, price) values ('1', 'keyboard', '120');insert into 104_Products (product_id, product_name, price) values ('2', 'mouse', '80');insert into 104_Products (product_id, product_name, price) values ('3', 'screen', '600');insert into 104_Products (product_id, product_name, price) values ('4', 'hard disk', '450');
最终SQL:
-- 方法一selectp1.product_name,o2.product_id,o2.order_id,o2.order_datefrom104_Products p1right join(selecto2.product_id,o2.order_date,o2.order_idfrom104_Orders o2join(selecto.product_id,max(order_date) as order_date,max(order_id) as order_idfrom104_Orders ogroup byproduct_id) o1ono2.order_date = o1.order_dateando2.product_id = o1.product_id) o2onp1.product_id = o2.product_idorder byp1.product_name,o2.product_id,o2.order_id;-- 方法二selectproduct_name,product_id,order_id,order_datefrom(selectproduct_name,o.product_id,order_id,order_date,rank() over(partition by o.product_id order by order_date desc) rkfrom104_Orders oleft join104_Products pono.product_id =p.product_id )t1whererk =1order byproduct_name,product_id,order_id;
