LeetCode:Department Highest Salary - 部门内最高工资

1、题目名称

Department Highest Salary(部门内最高工资)

2、题目地址

https://leetcode.com/problems/rising-temperature

3、题目内容

表Employee包括四列:Id、Name、Salary、DepartmentId

+----+-------+--------+--------------+
| Id | Name  | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 70000  | 1            |
| 2  | Henry | 80000  | 2            |
| 3  | Sam   | 60000  | 2            |
| 4  | Max   | 90000  | 1            |
+----+-------+--------+--------------+

表Department表中包括了公司的所有部门

+----+----------+
| Id | Name     |
+----+----------+
| 1  | IT       |
| 2  | Sales    |
+----+----------+

现要求写一个SQL,求出某一部门内领取最高工资的员工。

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| Sales      | Henry    | 80000  |
+------------+----------+--------+

4、初始化数据库脚本

在MySQL数据库中建立一个名为LEETCODE的数据库,用MySQL命令行中的source命令执行下面脚本:

-- 执行脚本前必须建立名为LEETCODE的DATABASE
USE LEETCODE;
 
DROP TABLE IF EXISTS Employee;
CREATE TABLE Employee (
  Id INT NOT NULL PRIMARY KEY,
  Name VARCHAR(50),
  Salary INT,
  DepartmentId INT
);

INSERT INTO Employee (Id, Name, Salary, DepartmentId) VALUES (1, ‘Joe‘, 70000, 1);
INSERT INTO Employee (Id, Name, Salary, DepartmentId) VALUES (2, ‘Henry‘, 80000, 2);
INSERT INTO Employee (Id, Name, Salary, DepartmentId) VALUES (3, ‘Sam‘, 60000, 2);
INSERT INTO Employee (Id, Name, Salary, DepartmentId) VALUES (4, ‘Max‘, 90000, 1);

DROP TABLE IF EXISTS Department;
CREATE TABLE Department (
  Id INT NOT NULL PRIMARY KEY,
  Name VARCHAR(50)
);

INSERT INTO Department (Id, Name) VALUES (1, ‘IT‘);
INSERT INTO Department (Id, Name) VALUES (2, ‘Sales‘);

5、解题SQL1

一个思路是,先求出各个部门的最高工资,再考察Employee表中,工资数等于所在部门的最高工资的员工,即为所求。

SELECT D.Name AS Department, E.Name AS Employee, MAXS_D.MAXS AS Salary
FROM Employee AS E, Department AS D, (
  SELECT MAX(Salary) AS MAXS, DepartmentId
  FROM Employee
  GROUP BY DepartmentId) AS MAXS_D
WHERE E.Salary = MAXS_D.MAXS AND E.DepartmentId = D.Id AND 
  E.DepartmentId = MAXS_D.DepartmentId;

6、解题SQL2

另一个思路是,在同一部门中,工资大于等于该部门所有员工的员工,即为所求。

SELECT D.Name AS Department, E.Name AS Employee, E.Salary AS Salary
FROM Employee AS E, Department AS D
WHERE E.DepartmentId = D.Id AND Salary >= ALL(
  SELECT Salary 
  FROM Employee E_TMP
  WHERE E_TMP.DepartmentId = E.DepartmentId);

END

时间: 2024-10-20 09:08:57

LeetCode:Department Highest Salary - 部门内最高工资的相关文章

[LeetCode] Department Highest Salary -- 数据库知识(mysql)

184. Department Highest Salary The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id. +----+-------+--------+--------------+ | Id | Name | Salary | DepartmentId | +----+-------+--

[LeetCode] Department Highest Salary 系里最高薪水

The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id. +----+-------+--------+--------------+ | Id | Name | Salary | DepartmentId | +----+-------+--------+--------------+ | 1 | Jo

[LeetCode]Department Highest Salary,解题报告

题目 The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id. Id Name Salary DepartmentId 1 Joe 70000 1 2 Henry 80000 2 3 Sam 60000 2 4 Max 90000 1 The Department table holds all depa

LeetCode - Department Highest Salary

题目大概的意思是选出每个Department里工资最高的人的信息并组成相应的表信息 有几个值得注意的地方:1)使用group by语句时,前面的select语句后面的内容只能有两种情况一种是group by后面的属性,另一种是聚集函数. 2)在选取最大Salary时必须使用e1.Salary=e2.Salary and e1.DepartmentId=e2.DepartmentId两个条件,要不然会有重复. 基于这些考虑可以使用派生表查询来找出最大Salary,然后与Department表做自然

LeetCode:Second Highest Salary - 第二高的工资

1.题目名称 Second Highest Salary(第二高的工资) 2.题目地址 https://leetcode.com/problems/second-highest-salary/ 3.题目内容 现在有一张记录了Id(主键)和Salary(工资)的表,求出其中第二高的工资.如果不存在第二高的工资,返回null. +----+--------+ | Id | Salary | +----+--------+ | 1  | 100    | | 2  | 200    | | 3  | 

Department Highest Salary

The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id. +----+-------+--------+--------------+ | Id | Name | Salary | DepartmentId | +----+-------+--------+--------------+ | 1 | Jo

[LeetCode]-DataBase-Department Highest Salary

The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id. +----+-------+--------+--------------+ | Id | Name | Salary | DepartmentId | +----+-------+--------+--------------+ | 1 | Jo

184. Department Highest Salary (medium)

Source: https://leetcode.com/problems/department-highest-salary/#/descriptionDescription: The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id. +----+-------+--------+-----------

[LeetCode]-DataBase-Nth Highest Salary

Write a SQL query to get the nth highest salary from the Employee table. +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+ For example, given the above Employee table, the nth highest salary where n =