SQL 从员工表中选择前 10 名薪水

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24767679/
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 02:14:07  来源:igfitidea点击:

Select top 10 salary from employees table

sqloracle

提问by Softwarex3

I wrote this sql query:

我写了这个sql查询:

select first_name, salary 
from employees 
where salary in( select distinct top(10) salary from employees order by salary  disc );

When I ran it, I got this error:

当我运行它时,我收到了这个错误:

SQL Error: ORA-00907: missing right parenthesis 00907. 00000 - "missing right parenthesis"

SQL 错误:ORA-00907:缺少右括号 00907. 00000 - “缺少右括号”

What could have caused the error?

什么可能导致错误?

回答by Kirill Leontev

Top-N query is typically performed this way in Oracle:

在 Oracle 中,Top-N 查询通常以这种方式执行:

select * from (
select first_name, salary 
from employees order by salary desc
) where rownum <= 10

This one gets you top 10 salaries.

这个可以让你获得前 10 名的薪水。

回答by Patrick Hofman

I think the problem lies in the use of topwhich is SQL Server and not Oracle.

我认为问题在于使用的top是SQL Server而不是Oracle。

Use rankinstead to get the salary in the decent order and get the first 10 of them:

rank改为使用以获取体面顺序的工资并获取其中的前 10 个:

select v.first_name, v.salary 
from ( select first_name, salary, rank() over (order by salary desc) r from employees) v
where v.r <= 10

回答by TMNT2014

Try -

尝试 -

SELECT first_name, salary 
(  select first_name, salary 
   from employees 
   order by salary Desc)
where rownum <= 10

回答by Rahul

The below Query works in Oracle.

以下查询适用于 Oracle。

select * from (select * from emp order by sal desc) where rownum<=10;

select * from (select * from emp order by sal desc) where rownum<=10;

回答by Tech.Kabir

Try this === SELECT first_name, salary FROM employees WHERE salary IN (SELECT salary FROM employees GROUP BY salary ORDER BY salary DESC LIMIT 10);

试试这个=== SELECT first_name,salary FROM员工WHEREsalary IN(SELECT工资FROM员工GROUP BY工资ORDER BY工资DESC LIMIT 10);