zoukankan      html  css  js  c++  java
  • [LeetCode]-DataBase-Department Highest Salary

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

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

    需求:查询每个部门工资最高的员工

    CREATE TABLE Employee(
    Id TINYINT UNSIGNED,
    Name VARCHAR(20),
    Salary DECIMAL(10,2),
    DepartmentId TINYINT
    )ENGINE=MyISAM CHARSET=utf8;
    CREATE TABLE Department(
    Id TINYINT UNSIGNED,
    Name VARCHAR(20)
    )ENGINE=MyISAM CHARSET=utf8;


    SELECT b.nm,a.Name,a.salary
    FROM employee a INNER JOIN (
    SELECT t2.Id,t2.Name nm,MAX(t1.salary) sal
    FROM employee t1 INNER JOIN department t2 ON t1.DepartmentId=t2.Id
    GROUP BY t1.DepartmentId
    )b ON a.salary=b.sal AND a.DepartmentId=b.Id

  • 相关阅读:
    C&Pointer求解wc问题
    软件测试作业2
    第六周小组作业
    WordCount改进 小组项目
    WordCount
    我的“游戏”人生
    软件测试第6周小组作业
    软件测试第4周小组作业:WordCount优化
    软件测试第二周个人作业:WordCount
    MVC模式下基于SSH三大框架的java web项目excel表格的导出(不依赖另外的jar包)
  • 原文地址:https://www.cnblogs.com/lianliang/p/5303064.html
Copyright © 2011-2022 走看看