力扣sql入门篇(一)
1 两人之间的通话天数
1.1 题目内容
1.1.1 基本题目信息
1.1.2 示例输入输出
1.2 示例sql语句
SELECT from_id person1, to_id person2, count ( * ) call_count, sum ( duration) total_duration
FROM Calls
GROUP BY least( from_id, to_id) , greatest( from_id, to_id) ;
1.3 运行截图
2 平均售价
2.1 题目内容
2.1.1 基本题目信息
2.1.2 示例输入输出
2.2 示例sql语句
SELECT p1. product_id, ROUND ( p1. s_price/ u1. s_number, 2 ) average_price
FROM
( SELECT p. product_id, sum ( u. units* p. price) s_priceFROM Prices pLEFT JOIN UnitsSold uON p. product_id= u. product_id AND u. purchase_date BETWEEN p. start_date AND p. end_date GROUP BY p. product_id
) p1
LEFT JOIN
( SELECT product_id, sum ( units) s_numberFROM UnitsSold GROUP BY product_id
) u1
ON p1. product_id= u1. product_id
2.3 运行截图
3 仓库经理
3.1 题目内容
3.1.1 基本题目信息
3.1.2 示例输入输出
3.2 示例sql语句
SELECT w. name warehouse_name, SUM ( w. units* p. stock) volume
FROM Warehouse w
LEFT JOIN
( SELECT product_id, Width* Length* Height stockFROM Products
) p
ON w. product_id= p. product_id
GROUP BY w. name
3.3 运行截图
4 苹果和桔子
4.1 题目内容
4.1.1 基本题目信息
4.1.2 示例输入输出
4.2 示例sql语句
SELECT s1. sale_date, s1. sold_num- s2. sold_num diff
FROM Sales s1
INNER JOIN Sales s2
ON s1. sale_date= s2. sale_date AND s1. fruit= 'apples' AND s1. fruit <> s2. fruit
ORDER BY s1. sale_date;
4.3 运行截图