如何获得最高薪水以及有员工姓名?

时间:2016-07-16 19:23:10

标签: mysql sql

我有这样的表。

enter image description here 我想找到明智的最高工资部门和拥有它的员工的姓名。 我运行了MySql查询

select concat(First_name,' ',Last_name) as Name,max(SALARY) 
from Employee 
group by Department;

给出如下所示的结果。

enter image description here

其中max(SALARY)是正确的但Emplyoee名称是错误的。如何才能正确?

5 个答案:

答案 0 :(得分:2)

试试这个:

SELECT concat(First_name,' ',Last_name) as Name,SALARY FROM Employee WHERE salary IN (SELECT MAX(SALARY) FROM Employee GROUP BY Department);

这会对你有帮助。

答案 1 :(得分:1)

如果只想要最大

#!/bin/bash

# The purpose of this script is to do things with files generated by
# 'create-react-app' after 'build' is run.
# 1. Move files to a new directory called 'user'
#    The resulting structure is 'build/user/static/<etc>'
# 2. Update reference on generated files from
#    static/<etc>
#     to
#    user/static/<etc>
#
# More details on: https://github.com/facebook/create-react-app/issues/3824

# Browse into './build/' directory
cd build
# Create './user/' directory
echo '1/4 Create "user" directory'
mkdir user
# Find all files, excluding (through 'grep'):
# - '.',
# - the newly created directory './user/'
# - all content for the directory'./static/'
# Move all matches to the directory './user/'
echo '2/4 Move relevant files'
find . | grep -Ev '^.$|^.\/user$|^.\/static\/.+' | xargs -I{} mv -v {} user
# Browse into './user/' directory
cd user
# Find all files within the folder (not subfolders)
# Replace string 'static/' with 'user/static/' on all files that match the 'find'
# ('sed' requires one to create backup files on OSX, so we do that)
echo '3/4 Replace file references'
find . -type f -maxdepth 1 | LC_ALL=C xargs -I{} sed -i.backup -e 's,static/,user/static/,g' {}
# Delete '*.backup' files created in the last process
echo '4/4 Clean up'
find . -name '*.backup' -type f -delete
# Done

如果需要最大值和最小值

SELECT first_name,department,salary FROM Employees WHERE salary IN (SELECT max(salary)  From employees GROUP BY department)

答案 2 :(得分:0)

您必须找出每个部门的最高工资,然后找到该部门中有该工资的员工。

使用CTE或相关子查询。

With myCTE (dept, maxsalary)
as (
Select dept, max(salary) over (partition by dept) from EmployeeTable)
Select concat(e.firstname, e.lastname), e. salary
from EmployeeTable e where e.Dept = myCTE.dept and e.Salary = myCTE.salary

答案 3 :(得分:0)

这是一个正确的解决方案:

SELECT concat(e.First_name, ' ', e.Last_name) as Name, e.SALARY
FROM Employee e
WHERE salary = (SELECT MAX(SALARY) FROM Employee e2 WHERE e2.Department = e.Department);

答案 4 :(得分:0)

SELECT t.name,t.dept_id,temp.max
FROM employee as t 
JOIN (SELECT dept, MAX(salary) AS max
FROM employee
GROUP BY dept_id) as temp
on t.dept_id=temp.dept_id and t.salary=temp.max