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  |

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

  • 相关阅读:
    VUE assets里的scss没有引用会被打包进代码里,本地代码和打包后的代码样式不一致解决办法
    echarts图表配置
    关于哈希路由多项目部署同一个服务器的链接访问问题
    git操作失误,提交代码因为网络问题没有成功,然后操作时候点错按钮导致代码全部没有了,也没用备份,如何解决
    浏览器刷新时候不删除信息,关闭后删除用户信息处理办法,浏览器监听刷新以及删除事件、cookie、session、sessionStorage、localStorage区别
    angular打包部署设置publicPath文件目录及访问地址,解决打包完成后,运行打包文件,报错404,js,css未找到
    Oracle spatial与arcsde 的关系
    Oracle Spatial图层元数据坐标范围影响R-TREE索引的ROOT MBR吗?
    centos下安装supervisor的步骤详解
    laravel 队列
  • 原文地址:https://www.cnblogs.com/heyang78/p/12864230.html
Copyright © 2011-2022 走看看