简体   繁体   中英

How to select nth row from table using rownum

SELECT emp_id INTO high_payed_emp_id
FROM (SELECT emp_id  from cursor_table ORDER BY emp_salary DESC)
WHERE rownum = 1;

I am trying to get the emp_id of max. payed employee error message:ORA-00905: missing keyword

Use FETCH FIRST instead, add WITH TIES to get both if there are two (or more) with the same max salary:

SELECT emp_id
FROM cursor_table
ORDER BY emp_salary DESC
FETCH FIRST 1 ROW WITH TIES

Yor need correct sql syntax

 insert INTO  high_payed_emp_id (emp_id)
 select * from
 (
 SELECT emp_id
 FROM cursor_table ORDER BY emp_salary DESC
  )
 WHERE ROWNUM <= 1;
SELECT emp_id INTO high_payed_emp_id
FROM (SELECT emp_id  from cursor_table 
WHERE emp_salary = (select max(emp_salary) from cursor_table))a

This seems to me the easiest approach to select the employees having max salary in Oracle:

select emp_id from cursor_table where emp_salary 
= (select max(emp_salary) from cursor_table)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM