2010-05-31 101 views

回答

9
SELECT *FROM 
    (
    SELECT *FROM emp 
    ORDER BY Salary desc 
    ) 
WHERE rownum <= 3 
ORDER BY Salary ; 
+0

爲什麼在WHERE之後需要ORDER BY薪水? – Samra 2014-01-29 04:24:30

+0

,因爲薪水將按降序排列,沒有它 – Raja 2016-04-14 17:49:15

3

像下面這樣的東西應該這樣做。

SELECT Name, Salary 
FROM 
    (
    SELECT Name, Salary 
    FROM   emp 
    ORDER BY Salary desc 
    ) 
WHERE rownum <= 3 
ORDER BY Salary ; 
+1

簡單的回答一個愚蠢的問題。 – amphetamachine 2010-05-31 12:19:46

12

你可以試試。

SELECT * FROM 
    (
     SELECT EMPLOYEE, LAST_NAME, SALARY, 
     RANK() OVER (ORDER BY SALARY DESC) EMPRANK 
     FROM emp 
    ) 
    WHERE emprank <= 3; 

這將給予正確的輸出,即使有兩個員工一樣的工資maximun

-1
SELECT salary,first_name||' '||last_name "Name of the employee" 
FROM hr.employees 
WHERE rownum <= 3 
ORDER BY salary desc ; 
+0

這肯定會給出前3名工資獲得僱員的名字以及他們的工資 – 2012-02-25 09:42:31

+0

-1這將無法可靠地工作,請參見[手冊](http://docs.oracle.com) .com/cd/E11882_01/server.112/e26088/pseudocolumns009.htm#i1006297)以解釋爲什麼不應該以這種方式使用ROWNUM。 – 2013-07-05 15:31:54

0
SELECT * FROM 
    (
     SELECT EMPLOYEE, LAST_NAME, SALARY, 
     DENSE_RANK() OVER (ORDER BY SALARY DESC) EMPRANK 
     FROM emp 
    ) 
    WHERE emprank <= 3; 
3
SELECT a.ename, b.sal 
    FROM emp a, emp b 
    WHERE a.empno = b.empno 
      AND 
      3 > (SELECT count(*) FROM emp b 
        WHERE a.sal = b.sal); 

不使用TOP,ROWID,排名等與最古老的SQL還

作品
0
Select ename, job, sal from emp 
    where sal >=(select max(sal) from emp 
    where sal < (select max(sal) from emp 
    where sal < (select max(sal) from emp))) 
    order by sal; 

ENAME  JOB    SAL 
---------- --------- ---------- 
KING  PRESIDENT  5000 
FORD  ANALYST   3000 
SCOTT  ANALYST   3000 
-2
select top 3 * from emp order by sal desc 
-1
select empno,salary from emp e 
    where 3 > (Select count(salary) from emp 
     where e.salary < salary) 


Another way : 

    select * from 
    (
    select empno,salary, 
    Rank() over(order by salary desc) as rank from emp) 
    where Rank <= 3; 

Another Way : 

    select * from 
    (
    select empno,salary from emp 
    order by salary desc 
    ) 
    where rownum <= 3; 
+0

請格式化您發佈的SQL。選擇代碼並點擊{}按鈕。 – OCDan 2013-07-05 12:55:50

+0

-1此答案不添加任何新信息。 – 2013-07-05 15:32:56

-1
SELECT * FROM 
    (
     SELECT ename, sal, 
     DENSE_RANK() OVER (ORDER BY SAL DESC) EMPRANK 
     FROM emp 
    ) 
    emp1 WHERE emprank <=5 
-2
SELECT DISTINCT(salary) FROM emp order by salary asc limit 0 ,3 

上面的查詢給出了DISTINCT三個最高的薪水。

-1
SELECT Name, Salary 
FROM 
    (
    SELECT Name, Salary 
    FROM   emp 
    ORDER BY Salary desc 
    ) 
WHERE rownum <= 3 
ORDER BY Salary ; 
-2
SELECT * FROM Employees 
WHERE rownum <= 3 
ORDER BY Salary ; 
-2

溶液用於通過尋找前5薪水平方英寸升服務器從薪金順序

選擇頂(1)的名稱,從薪金薪水其中薪水(選擇不同的頂部(3)的工資薪水盤)

+1

請嘗試充實你的答案,並改進格式。還要考慮你的答案是否與其他答案中缺乏的信息有關。 – 2014-05-02 21:16:43

-1

選擇頂部(3)分鐘(名稱),TotalSalary,ROW_NUMBER()OVER(由TotalSalary倒序)AS ROWNUMBER FROM tbl_EmployeeProfile組由TotalSalary

-1

限制查詢僅顯示前3名最高薪酬員工。 :查詢«甲骨文的PL/SQL

create table employee(
     emp_no     integer   primary key 
     ,lastname    varchar2(20) not null 
     ,salary     number(3) 
); 

insert into employee(emp_no,lastname,salary) 
       values(1,'Tomy',2); 

insert into employee(emp_no,lastname,salary) 
       values(2,'Jacky',3); 

insert into employee(emp_no,lastname,salary) 
       values(3,'Joey',4); 

insert into employee(emp_no,lastname,salary) 
       values(4,'Janey',5); 


select lastname, salary 
from (SELECT lastname, salary FROM employee ORDER BY salary DESC) 
where rownum <= 3 ; 

輸出

LASTNAME     SALARY 

-------------------- ---------- 
Janey       5 

Joey       4 

Jacky       3 

drop table employee; 
0

你可以使用DBMS_STAT_FUNCS.Summary

SET SERVEROUTPUT ON; 
DECLARE 
    s DBMS_STAT_FUNCS.SummaryType; 
BEGIN 
    DBMS_STAT_FUNCS.SUMMARY('HR', 'EMPLOYEES', 'SALARY',3, s); 
    DBMS_OUTPUT.put_line('Top 3: ' || s.top_5_values(1) || '-' 
         || s.top_5_values(2) || '-' || s.top_5_values(3)); 
END; 
/

輸出:

Top 3: 24000-17000-17000 
相關問題