what is the query to return Name and Salary of employee Having Max Salary

邮差的信 提交于 2019-12-10 11:05:23

问题


  1. what is the query to return Name and Salary of employee Having Max Salary


回答1:


SELECT Name, Salary FROM Minions
WHERE Salary = (SELECT Max(Salary) FROM Minions)

Note that this will return more than one row if there is more than one employee who have the same max salary




回答2:


select name, salary from (select * from salary_table order by salary desc limit 1)



回答3:


SELECT FirstName, max(salary)
FROM Employees 
WHERE salary = (
                SELECT max(salary)
                FROM employees
               )
GROUP BY FirstName

working in SQL SERVER 2012




回答4:


Select e.name, e.salary from employee e where
  not exists (select salary from employee e1 where e.salary < e1.salary)

This will of course return more than one record if there are multiple people with the max salary.




回答5:


A couple of proprietary solutions

SELECT TOP 1 [WITH ties] Name, Salary
FROM employee
ORDER BY  Salary DESC


SELECT Name, Salary
FROM employee
ORDER BY  Salary DESC
LIMIT 1

And a standard one

WITH E AS
(
    SELECT Name, Salary, 
    ROW_NUMBER() OVER (ORDER BY Salary DESC) RN /*Or RANK() for ties*/
    FROM employee
)
SELECT Name, Salary FROM E WHERE RN=1



回答6:


If you are using an oracle database and only want a single "employee" then:

SELECT MAX( name   ) KEEP ( DENSE_RANK LAST ORDER BY salary ASC ) AS name,
       MAX( salary ) KEEP ( DENSE_RANK LAST ORDER BY salary ASC ) AS salary
FROM   Minions;

SQLFIDDLE

(kudos to Neil N for his table name)

  • SQL Server has a similar FIRST_VALUE (or LAST_VALUE) analytic function.
  • PostgreSQL also supports window functions including LAST_VALUE.



回答7:


These type of queries (grouped operaions) can execute with sub query. Example

select *from emp where sal=(max(sal)from emp)



回答8:


In case there are multiple rows with the same MAX number then you can just limit the number to desired number like this

SELECT Name, Salary FROM Minions
WHERE Salary = (SELECT Max(Salary) FROM Minions) LIMIT 1


来源:https://stackoverflow.com/questions/3355744/what-is-the-query-to-return-name-and-salary-of-employee-having-max-salary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!