目录
1.上级经理已离职的公司员工
2.修复表中的名字
3. 寻找用户推荐人
4.产品销售分析 I
5.平均售价
1.上级经理已离职的公司员工
子查询。
先根据薪水大小查询,再根据manager_id查询该员工是否存在,最后做排序。
# Write your MySQL query statement below
SELECT
employee_id
FROM
Employees
WHERE
salary<30000
AND
manager_id not in (SELECT employee_id employee_id FROM Employees)
ORDER BY
employee_id
2.修复表中的名字
SQL语句的使用
# Write your MySQL query statement below
#重命名为name
SELECT user_id,CONCAT(UPPER(SUBSTRING(name,1,1)),LOWER(SUBSTRING(name,2))) name
FROM Users
ORDER BY user_id
3. 寻找用户推荐人
查询题。
# Write your MySQL query statement below
SELECT name FROM customer
WHERE id NOT IN
(SELECT id FROM customer WHERE referee_id=2);
4.产品销售分析 I
连接题。
# Write your MySQL query statement below
SELECT p.product_name,s.year,s.price
FROM Sales s
LEFT JOIN Product p
ON s.product_id=p.product_id
5.平均售价
聚合函数题。
# Write your MySQL query statement below
SELECT p.product_id,ROUND(SUM(p.price*u.units)/SUM(u.units),2)
AS average_price FROM Prices AS p
JOIN UnitsSold AS u
ON p.product_id=u.product_id
AND u.purchase_date BETWEEN p.start_date AND p.end_date
GROUP BY p.product_id
UNION ALL SELECT product_id, 0 AS average_price FROM Prices WHERE product_id NOT IN ( SELECT DISTINCT product_id FROM UnitsSold )