返回具有最高薪水的员工姓名和薪水的查询是什么
答案 0 :(得分:15)
SELECT Name, Salary FROM Minions
WHERE Salary = (SELECT Max(Salary) FROM Minions)
请注意,如果有多个员工具有相同的最高薪水
,则会返回多行答案 1 :(得分:3)
select name, salary from (select * from salary_table order by salary desc limit 1)
答案 2 :(得分:1)
SELECT FirstName, max(salary)
FROM Employees
WHERE salary = (
SELECT max(salary)
FROM employees
)
GROUP BY FirstName
在SQL SERVER 2012中工作
答案 3 :(得分:0)
Select e.name, e.salary from employee e where
not exists (select salary from employee e1 where e.salary < e1.salary)
如果有多个人拥有最高工资,这当然会返回多条记录。
答案 4 :(得分:0)
一些专有解决方案
SELECT TOP 1 [WITH ties] Name, Salary
FROM employee
ORDER BY Salary DESC
SELECT Name, Salary
FROM employee
ORDER BY Salary DESC
LIMIT 1
标准的
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
答案 5 :(得分:0)
如果您使用的是oracle数据库,并且只需要一个“员工”,那么:
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;
(感谢Neil N的桌名)
FIRST_VALUE
(或LAST_VALUE
)分析函数。LAST_VALUE
。答案 6 :(得分:0)
这些类型的查询(分组操作)可以与子查询一起执行。 例如,从emp中选择*,其中sal =(从emp中的max(sal))
答案 7 :(得分:0)
如果有多行具有相同的 MAX 数字,则可以将数字限制为所需的数字,例如
SELECT Name, Salary FROM Minions
WHERE Salary = (SELECT Max(Salary) FROM Minions) LIMIT 1
答案 8 :(得分:0)
假设只有一名员工的最高工资
SQL Server:
select top 1 name, salary
from employee
order by salary desc
Oracle:
select name, salary
from employee
order by salary desc
limit 1
与使用子查询的其他查询不同,上述查询仅扫描表一次。