前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 0185 - Department Top Three Salaries

LeetCode 0185 - Department Top Three Salaries

作者头像
Reck Zhang
发布2021-08-11 14:55:19
2260
发布2021-08-11 14:55:19
举报
文章被收录于专栏:Reck ZhangReck Zhang

Department Top Three Salaries

Desicription

The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id.

代码语言:javascript
复制
+----+-------+--------+--------------+
| 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.

代码语言:javascript
复制
+----+----------+
| 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.

代码语言:javascript
复制
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| IT         | Randy    | 85000  |
| IT         | Joe      | 70000  |
| Sales      | Henry    | 80000  |
| Sales      | Sam      | 60000  |
+------------+----------+--------+

Solution

代码语言:javascript
复制
SELECT 
    D.Name as Department,
    E.Name as Employee,
    E.Salary
FROM
    Department D,
    Employee E
WHERE
    E.DepartmentId = D.Id AND
    (SELECT 
        COUNT(DISTINCT Salary)
    FROM 
        Employee
    WHERE
        DepartmentId = D.Id AND
        Salary > E.Salary) < 3
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-06-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Department Top Three Salaries
    • Desicription
      • Solution
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档