开发者

How to return multiple columns when using group by in SQL

I want the names of the employees along with their salary and department who have maximum salary less than 50,000. ihave the following

SELECT department, MAX(salary) as Highest salary
FROM employees
GROUP BY department开发者_StackOverflow中文版
HAVING MAX(salary) < 50000

How do I get the name of the employee to be returned?


So close...

SELECT department, name, MAX(salary) as Highest salary
FROM employees
GROUP BY department, name
HAVING MAX(salary) < 50000

After comment updates

SELECT name, department , salary
FROM employees e
   JOIN
  (
    SELECT department as dept, MAX(salary) as HighestSalary
    FROM employees
    GROUP BY department
  ) MaxE ON e.department = MaxE.dept AND e.salary = MaxE.HighestSalary


Unless I'm misunderstanding the requirement, just add employee to the select/group by?

SELECT employee, department, MAX(salary) as Highest salary
FROM employees
GROUP BY employee, department
HAVING MAX(salary) < 50000
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜