首页 > 解决方案 > 如何在sql中使用group by、order by和where条件?

问题描述

给定表包含

employee_id, 
Month_Year, 
Salary, 
City

编写查询以获得每个城市的第五高年薪员工。

我的代码:

select * from (select * from(select * from (select * from dummy order by sal desc) where rownum <= 5)  order by sal) where rownum <=1 ;

但这仅在总表中排名第五。

我想在哪里使用 group by 语句?

提前致谢。

标签: sqloracle

解决方案


你可以试试下面 -

select *
from tablename as t1 
where 
  t1.salary = (select salary from tablename t2
             where 
             t2.city= t1.city ORDER by salary desc limit 1 offset 5)

推荐阅读