leetcode 185. Department Top Three Salaries

来源:互联网 发布:淘宝steam 编辑:程序博客网 时间:2024/05/16 09:40

185. Department Top Three Salaries

 
 My Submissions
  • Total Accepted: 7471
  • Total Submissions: 49037
  • Difficulty: Hard

The Employee table 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 Department table 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  |+------------+----------+--------+

Subscribe to see which companies asked this question

# Write your MySQL query statement belowselect d.Name as Department, e.Name as Employee, e.Salary as Salary    from Employee e inner join Department d on e.DepartmentId = d.Id    where 3>(select count(distinct e1.Salary)            from Employee e1            where e1.Salary>e.Salary            and e1.DepartmentId = e.DepartmentId)


0 0
原创粉丝点击