leetcode 184,185 Department Top Three Salaries/HIGHEST Salaries
2015-01-28 23:48
337 查看
The
Employeetable holds all employees. Every employee has an Id, 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 | | 5 | Janet | 69000 | 1 | | 6 | Randy | 85000 | 1 | +----+-------+--------+--------------+The
Departmenttable holds all departments of the company.
+----+----------+ | Id | Name | +----+----------+ | 1 | IT | | 2 | Sales | +----+----------+Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows.
+------------+----------+--------+ | Department | Employee | Salary | +------------+----------+--------+ | IT | Max | 90000 | | IT | Randy | 85000 | | IT | Joe | 70000 | | Sales | Henry | 80000 | | Sales | Sam | 60000 | +------------+----------+--------+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 |+----+-------+--------+--------------+[/code]TheDepartment Highest Salary
Total Accepted: 546 Total Submissions: 2921My SubmissionsQuestion Solution The [code]Employee
Departmenttable 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 | +------------+----------+--------+
这两道题目的解法有些类似,就是where的条件有所不同,都使用了inner join。。on与my sql 用户自定义两种方法,不同之处在于where语句的稍微不同,
一个是t.rank<=3 求出前三个最大值,一个是t.rank=1排名第一的值
解法
SELECT d.NAME AS Department, t.NAME AS Employee, Salary FROM ( SELECT DepartmentId, NAME, Salary, @rank := IF(@prevDeptId != DepartmentId, 1, IF(@prevSalary = Salary, @rank, @rank + 1) ) AS Rank, @prevDeptId := DepartmentId AS prevDeptId, @prevSalary := Salary AS prevSalary FROM Employee e, (SELECT @rank := 0, @prevDeptId := NULL, @prevSalary := NULL) r ORDER BY DepartmentId ASC, Salary DESC) t INNER JOIN Department d ON t.DepartmentId = d.IDWHERE t.rank <= 3
# Write your MySQL query statement belowSELECT d.NAME AS Department, t.NAME AS Employee, Salary FROM ( SELECT DepartmentId, NAME, Salary, @rank := IF(@prevDeptId != DepartmentId, 1, IF(@prevSalary = Salary, @rank, @rank + 1) ) AS Rank, @prevDeptId := DepartmentId AS prevDeptId, @prevSalary := Salary AS prevSalary FROM Employee e, (SELECT @rank := 0, @prevDeptId := NULL, @prevSalary := NULL) r ORDER BY DepartmentId ASC, Salary DESC) t INNER JOIN Department d ON t.DepartmentId = d.IDWHERE t.rank =1
相关文章推荐
- 【LeetCode】Department Highest Salary && Department Top Three Salaries
- LeetCode_OJ【185】Department Top Three Salaries
- leetcode185 Department Top Three Salaries
- leetcode185 Department Top Three Salaries
- LeetCode Department Top Three Salaries
- [LeetCode][SQL]Department Top Three Salaries
- 185. Department Top Three Salaries
- [LeetCode] Department Top Three Salaries 系里前三高薪水
- [leetcode] Department Top Three Salaries
- LeetCode | database | Department Top Three Salaries
- 185. Department Top Three Salaries
- 185. Department Top Three Salaries
- [LeetCode][Database]Department Top Three Salaries
- 185. Department Top Three Salaries
- [LeetCode]-DataBase-Department Top Three Salaries
- leetcode数据库sql之Department Top Three Salaries
- 题目9:MySQL---------Department Top Three Salaries
- Department Top Three Salaries
- Department Top Three Salaries
- Department Top Three Salaries