SQL 返回员工姓名和工资的查询是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3355744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 06:58:50  来源:igfitidea点击:

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

sqlgreatest-n-per-group

提问by user404835

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

  1. 返回员工姓名和工资的查询是什么

回答by Neil N

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

请注意,如果有多个员工具有相同的最高工资,这将返回多于一行

回答by publicRavi

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

回答by arpit

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

working in SQL SERVER 2012

在 SQL SERVER 2012 中工作

回答by M vijayapal reddy

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

这些类型的查询(分组操作)可以与子查询一起执行。例子

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

回答by BlackXero

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

如果有多行具有相同的MAX编号,则您可以将数字限制为所需的数字,如下所示

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

回答by user12963352

Assuming only one employee has maximum salary

假设只有一名员工有最高工资

SQL Server:

SQL 服务器:

select top 1 name, salary
from employee
order by salary desc

Oracle:

甲骨文:

select name, salary
from employee
order by salary desc
limit 1

Above queries scans the table only once unlike other queries using subqueries.

与使用子查询的其他查询不同,上述查询仅扫描一次表。

回答by Martin Smith

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

回答by MT0

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

如果您使用的是 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;

SQLFIDDLE

SQLFIDDLE

(kudos to Neil N for his table name)

(感谢 Neil N 的表名)

  • SQL Server has a similar FIRST_VALUE(or LAST_VALUE) analytic function.
  • PostgreSQL also supports window functions including LAST_VALUE.
  • SQL Server 具有类似FIRST_VALUE(或LAST_VALUE)分析功能。
  • PostgreSQL 还支持窗口函数,包括LAST_VALUE.

回答by deinst

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.

如果有多个人的最高工资,这当然会返回多个记录。