zoukankan      html  css  js  c++  java
  • Q200510-01: 求部门工资最高的员工

    问题: 求部门工资最高的员工

    Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id。

    +----+-------+--------+--------------+

    | 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 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max 在 IT 部门有最高工资,Henry 在 Sales 部门有最高工资。

    +------------+----------+--------+

    | Department | Employee | Salary |

    +------------+----------+--------+

    | IT         | Max      | 90000  |

    | Sales      | Henry    | 80000  |

    +------------+----------+--------+

    解答:

    mysql> select d.name as department,b.name as employee,b.salary from (select e.* from employee e inner join
        -> (select max(salary) as max_salary,departmentid from employee group by departmentid) a
        -> on e.salary=a.max_salary and e.departmentid=a.departmentid) b left join department d
        -> on b.departmentid=d.id ;
    +------------+----------+--------+
    | department | employee | salary |
    +------------+----------+--------+
    | Sales      | Henry    |  80000 |
    | IT         | Max      |  90000 |
    +------------+----------+--------+
    2 rows in set (0.00 sec)

    过程:

    create table employee(
        id int,
        name varchar(20) not null,
        salary int,
        departmentid int not null,
        primary key(id))
        
    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');
        
    create table department(
        id int,
        name varchar(20) not null,
        primary key(id))
        
    insert into department(id,name) values('1','IT');
    insert into department(id,name) values('2','Sales');
    
    select max(salary) as max_salary,departmentid from employee group by departmentid
    
    select e.* from employee e inner join 
    (select max(salary) as max_salary,departmentid from employee group by departmentid) a
    on e.salary=a.max_salary and e.departmentid=a.departmentid
    
    select b.id,b.name,b.salary,d.name from (select e.* from employee e inner join 
    (select max(salary) as max_salary,departmentid from employee group by departmentid) a
    on e.salary=a.max_salary and e.departmentid=a.departmentid) b left join department d
    on b.departmentid=d.id 
    
    select d.name as department,b.name as employee,b.salary from (select e.* from employee e inner join 
    (select max(salary) as max_salary,departmentid from employee group by departmentid) a
    on e.salary=a.max_salary and e.departmentid=a.departmentid) b left join department d
    on b.departmentid=d.id 

    --2020年5月10日 17点22分--

    1.       部门工资最高的员工

    Employee 表包含所有员工信息,每个员工有其对应的 Id, salary department Id

     

    +----+-------+--------+--------------+

    | 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 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max IT 部门有最高工资,Henry Sales 部门有最高工资。

     

    +------------+----------+--------+

    | Department | Employee | Salary |

    +------------+----------+--------+

    | IT         | Max      | 90000  |

    | Sales      | Henry    | 80000  |

    +------------+----------+--------+

  • 相关阅读:
    迭代器和生成器
    案例:复制大文件
    案例:使用seek倒查获取日志文件的最后一行
    Leetcode165. Compare Version Numbers比较版本号
    Leetcode137. Single Number II只出现一次的数字2
    Leetcode129. Sum Root to Leaf Numbers求根到叶子节点数字之和
    Leetcode116. Populating Next Right Pointers in Each Node填充同一层的兄弟节点
    Leetcode114. Flatten Binary Tree to Linked List二叉树展开为链表
    Leetcode113. Path Sum II路径总和2
    C++stl中vector的几种常用构造方法
  • 原文地址:https://www.cnblogs.com/heyang78/p/12864230.html
Copyright © 2011-2022 走看看