[英]SQL query to find out the name and salary of the employee who received highest salary for each month
[英]How to find top 2 distinct employee name with highest salary?
這是我的桌子
select emp_no, salary
from salaries
order by salary desc
emp_no, salary
'43624', '158220'
'43624', '157821'
'47978', '155709'
'109334', '155377'
'109334', '155190'
'109334', '154888'
'109334', '154885'
'80823', '154459'
'43624', '153458'
'43624', '153166'
'47978', '151929'
'80823', '151768'
'109334', '151484'
'43624', '151115'
'46439', '150345'
我用過查詢
select distinct emp_no, salary
from salaries
order by salary desc
limit 2
但它只返回同一用戶的前兩名工資**我需要前兩名最高員工 emp_no 的 emp_no 和他們的工資 **
您可以使用subquery
和max()
聚合函數。
select * from
(select emp_no, max(salary) maxsalary from salaries group by emp_no) t1
order by t1.maxsalary desc
limit 2
我認為這里不需要子查詢。 您可以直接聚合和排序:
select emp_no, max(salary) max_salary
from salaries
group by emp_no
order by max_salary desc
limit 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.