[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 departments of the company.

Id Name
1 IT
2 Sales

Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, Max has the highest salary in the IT department and Henry has the highest salary in the Sales department.

Department Employee Salary
IT Max 90000
Sales Henry 80000

思路

看到这到题目,我首先考虑的是数据库级联。具体思路如下:

  1. 查找每一部门的最高薪水。
select e.DepartmentId, MAX(e.Salary) as Salary, d.Name as Department from Employee as e inner join Department as d on e.DepartmentId = d.Id group by e.DepartmentId;

语句执行完成后,生成的表结构如下:

DepartmentId Salary Department
1 9000 IT
2 8000 Sales

2. 用上述生成的临时表和Employee表再做级联,找出题目要求的字段。

select t.Department as Department, e.Name as Employee, t.Salary as Salary from Employee as e inner join (1-sql生成的表) as t on e.Salary = t.Salary and and e.DepartmentId = t.DepartmentId;

AC SQL

最终的ac sql语句如下:

select t.Department as Department, e.Name as Employee, t.Salary as Salary from Employee as e inner join (select e.DepartmentId, MAX(e.Salary) as Salary, d.Name as Department from Employee as e inner join Department as d on e.DepartmentId = d.Id group by e.DepartmentId) as t on e.Salary = t.Salary and e.DepartmentId = t.DepartmentId;
时间: 2024-10-25 07:46:10

[LeetCode]Department Highest Salary,解题报告的相关文章

[LeetCode]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 = 2 is 200. If there is no nth highest salary, then the query should r

[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

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

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 | +----+-------+--------+--

LeetCode: Pascal's Triangle 解题报告

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] SOLUTION 1:很easy的题.注意记得把List加到ret中.比较简单,每一行的每一个元素有这个规律:1. 左右2边的是1.i, j 表示行,列坐标.2.

[LeetCode]Longest Valid Parentheses, 解题报告

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example i

【LeetCode】Insert Interval 解题报告

[题目] Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and m

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. +----+-------+--------+-----------