what is the query to return Name and Salary of emp

2019-05-26 03:45发布

问题:

  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. Ex.select *from emp where sal=(max(sal)from emp)