美文网首页
2020-04-10-(2)

2020-04-10-(2)

作者: DUYAN_bc77 | 来源:发表于2020-04-10 11:20 被阅读0次

    分组比较

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

    Screen Shot 2020-04-10 at 10.48.02 AM.png

    问题:
    编写一个 SQL 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max 在 IT 部门有最高工资,Henry 在 Sales 部门有最高工资。

    Screen Shot 2020-04-10 at 10.50.39 AM.png

    Solution

    SELECT DepartmentId, max( Salary ) 
    FROM Employee 
    GROUP BY DepartmentId 
    
    输出:分部门选出最高
    {"headers": ["DepartmentId", "max( Salary )"], 
       "values": [[1, 90000], [2, 80000]]}
    
    Select Department.Name As DepartmentId, Employee.Name As Employee, Salary
    From Employee,Department
    Where Employee.DepartmentId = Department.Id 
    
    输出:内连接
    {"headers": ["DepartmentId", "Employee", "Salary"], 
    "values": [["IT", "Joe", 70000], 
                    ["IT", "Jim", 90000], 
                    ["Sales", "Henry", 80000], 
                    ["Sales", "Sam", 60000],
                    ["IT", "Max", 90000]]}
    
    子查询(*And ID,Salary in*)
    Select Department.Name As Department, Employee.Name As Employee, Salary
    From Employee,Department
    Where Employee.DepartmentId = Department.Id 
    And (Employee.DepartmentId, Salary) IN (
        Select DepartmentId, max( Salary ) 
        From Employee 
        Group By DepartmentId)
    
    image.png

    相关文章

      网友评论

          本文标题:2020-04-10-(2)

          本文链接:https://www.haomeiwen.com/subject/wkilmhtx.html