[SQL]183+184+185+196+197

183.從不訂購的客戶

題目mysql

# Write your MySQL query statement below
SELECT Name AS Customers
FROM Customers LEFT JOIN Orders
ON Customers.Id = Orders.CustomerId
WHERE Orders.CustomerId IS NULL;

184. 部門工資最高的員工

題目sql

SELECT d.Name AS Department, e.Name AS Employee, e.Salary
FROM Department d, Employee e
WHERE e.DepartmentId = d.id
AND (e.Salary, e.DepartmentId) IN (SELECT max(Salary), DepartmentId
FROM Employee
GROUP BY DepartmentId);

185.部門工資前三高的員工

題目code

解答blog

SELECT d.Name AS Department, e1.Name AS Employee, e1.Salary
FROM Employee e1, Department d 
WHERE e1.DepartmentId = d.Id 
AND 3 > (SELECT COUNT(DISTINCT e2.Salary)
FROM Employee AS e2
WHERE e1.Salary < e2.Salary
AND e1.DepartmentId = e2.DepartmentId) -- primary key只能是Department,不能夠是Id
ORDER BY d.Name, Salary DESC;

我如今也不知道主鍵那裏究竟是爲何it

196. 刪除重複的電子郵箱

DELETE FROM Person
WHERE id NOT IN (SELECT need.id
    FROM ((SELECT MIN(Id) as id
        FROM Person
        GROUP BY Email) AS need));

197.上升的溫度

SELECT a.Id
FROM Weather a JOIN Weather b
ON a.Temperature > b.Temperature
AND DATEDIFF(a.RecordDate, b.RecordDate) = 1;
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息